简体   繁体   中英

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

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.. :)

单击项目,然后从中选择方向设置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM