After IOS6, if you want to support a horizontal screen in your project, we first need to add a setting that supports horizontal screen in the plist file, otherwise some code settings will be invalidated.
There is the way to set:
1. Add in Pilist supported interface orientations field
2. Check the settings in Xcode
Now let's look at several functions that determine the orientation of the screen:
Before IOS6, we only had to pass a function
-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation {
return (tointerfaceorientation = = uiinterfaceorientationlandscaperight);
}
You can support the rotation of the specified controller. With the new documentation, we can see:
Applications should use Supportedinterfaceorientations and/or shouldautorotate. -(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation NS_DEPRECATED_IOS ( 2_0, 6_0);//This method is marked as obsolete after 6.0
We take the following two methods instead of:
Whether to allow screen rotation
-(BOOL) shouldautorotate{
return YES;
}
Direction of support
-(Nsuinteger) supportedinterfaceorientations {
return uiinterfaceorientationmasklandscaperight;
}
This is an enumeration.
Typedef ns_options (Nsuinteger, uiinterfaceorientationmask) { uiinterfaceorientationmaskportrait = (1 << uiinterfaceorientationportrait), UIInterfaceOrientationMaskLandscapeLeft = (1 << Uiinterfaceorientationlandscapeleft), uiinterfaceorientationmasklandscaperight = (1 << uiinterfaceorientationlandscaperight), Uiinterfaceorientationmaskportraitupsidedown= (1 << uiinterfaceorientationportraitupsidedown), UIInterfaceOrientationMaskLandscape = ( uiinterfaceorientationmasklandscapeleft | uiinterfaceorientationmasklandScaperight), uiinterfaceorientationmaskall = ( uiinterfaceorientationmaskportrait | UIInterfaceOrientationMaskLandscapeLeft | uiinterfaceorientationmasklandscaperight | uiinterfaceorientationmaskportraitupsidedown), UIInterfaceOrientationMaskAllButUpsideDown = (Uiinterfaceorientationmaskportrait | UIInterfaceOrientationMaskLandscapeLeft | Uiinterfaceorientationmasklandscaperight),};
Through these two functions, if we need a controller to force the direction, we can set the support for a single direction, can achieve the purpose.
Attention:
If the Rootviewcontroller in your project is navigation, you will find that the code you added in the push-out view is not working because navigation is not set and we create a file that inherits from Navigationcontroller. In the inside rewrite the method just now, after doing this, the screen does cross over, and this navigation push all the sub-interface will be horizontal screen, this is not the effect we want. We want to be free to control the screen orientation of each push-out interface, which can be done in the navigation:
-(BOOL) shouldautorotate{return [Self.topviewcontroller shouldautorotate];} Supported directions-(Nsuinteger) supportedinterfaceorientations {return [Self.topviewcontroller supportedinterfaceorientations ];;}
We also need to do some processing, after my tests, the navigation must be after the pop to call these functions again, so my method is to do this: Pop an intermediate controller and then pop back.
@implementation viewcontroller2-(void) viewdidload {[Super viewdidload]; Do any additional setup after loading the view. [Self.navigationcontroller Pushviewcontroller:[[viewcontroller3 Alloc]init] animated:yes];}
@implementation viewcontroller3-(void) viewdidload {[Super viewdidload]; Do any additional setup after loading the view. [Self.navigationcontroller Popviewcontrolleranimated:yes];}
In doing so, we are free to control the direction of each view controller.
Similarly, if the root view controller is Tabbar, then we need to do the operation in Tabbar.
If we are mostly the view controller is a direction, only occasionally a few will be different, at this time, we can actually take the Presentationcontroller way, and then directly in the controller to write the two methods. This is the easiest way to do it.
The processing method of horizontal screen in IOS6