Some time ago to do a video project, landscape used during video playback, but set the landscape, allowing other interface will landscape, and landscape of my project is not fit, some would say, set up the only support portrait, landscape interface via code in need to control the landscape, it is a way, I say here is another way, first of all, Add the following code to the Appdelegate:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (self.allowRotation) {
        return UIInterfaceOrientationMaskAll;
    }

    return UIInterfaceOrientationMaskPortrait;

}
Copy the code

Also remember to add the property @property(nonatomic,assign)BOOL allowRotation; Then you can set it to allow rotation in any direction.

Use the Appdelegate singleton when an interface needs to be rotated: Remember to introduce the Appdelegate header file

    AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.allowRotation=YES;
Copy the code

Exit interface for step operation, even when the appdelegate. AllowRotation NO, ok, so you meet our requirements. You might as well try it yourself.

With that said, let’s talk about another method mentioned earlier. There are several methods:

// Set it to YES to allow rotation, you don't have to set the following, but you have to support vertical and horizontal screens, but usually you don't, because it will affect the whole project, and we need only a few landscape screens. -(BOOL)shouldAutorotate {return YES; } // Only after iOS6, instead of the following method, currently set to run landscape, if you do Unity, this is the case, as long as xcode has landscape checked, Otherwise it will collapse - (NSUInteger) directly supportedInterfaceOrientations {return UIInterfaceOrientationMaskLandscape; } // Deprecated in iOS6, Replaced by the above methods - (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation {return (interfaceOrientation == UIInterfaceOrientationMaskLandscape); }Copy the code

Want to rotate to the direction of the screen or want oneself each method to try, believe that tried to his understanding will be deeper, know how to use.