rob180
rob180

Reputation: 901

IOS7/IOS8 Allow only portrait in view controller

I am building an app for iPhone that will have only 1 landscape view, and so i want to block landscape for all other, i have tried this:

-(NSUInteger)supportedInterfaceOrientations
{
   return UIInterfaceOrientationMaskPortrait;
}

But it stills rotates

Upvotes: 2

Views: 2533

Answers (2)

valbu17
valbu17

Reputation: 4124

I will suggest to just make your app for portrait mode and then whenever you need the landscape mode then allow landscape mode.

First, as previously suggested click on -> Project name -> General -> Deployment Info -> Only select Portrait for Device Orientation.

Second, in your AppDelegate.h add this property..

@property (nonatomic) BOOL fullScreenVideoIsPlaying;

Then, on your AppDelegate.m I will add this function..

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    if (self.fullScreenVideoIsPlaying == YES) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

After doing this, in the view controller that you need landscape create a function or just add this code to your viewWillAppear method is depending how you want to accomplish this..

((AppDelegate *)[[UIApplication sharedApplication] delegate]).fullScreenVideoIsPlaying = YES;
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];

Then for setting back to portrait mode you do this..

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.fullScreenVideoIsPlaying = NO;

[self supportedInterfaceOrientations];

[self shouldAutorotate:UIInterfaceOrientationPortrait];

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];

You might need these functions for iOS 8..

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate:(UIInterfaceOrientation)interfaceOrientation{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

I hope it helps.. :)

Upvotes: 9

Yusuf terzi
Yusuf terzi

Reputation: 186

Click project and select Orientation settings from there.

Upvotes: -3

Related Questions