Method
1, through the Appdelegate-Application:supportedinterfaceorientationsforwindow: method to set
This method is to do, explain this method is used to set the interface to support the direction of the screen, is not used to turn the screen, this method does not meet part of the demand.
-(Uiinterfaceorientationmask) Application: (UIApplication *) application Supportedinterfaceorientationsforwindow: ( UIWindow *) Nowwindow {
if (_deviceorientationportrait) {return
uiinterfaceorientationmaskportrait;
}
return Uiinterfaceorientationmasklandscape | uiinterfaceorientationmaskportrait;
}
The _deviceorientationportrait is a bool attribute of the appdelegate when a vertical screen is required to display the interface:
In an interface, the following processing
-(void) Viewwillappear: (BOOL) animated {
[Super viewwillappear:animated];
[[Appdelegate delegate] setdeviceorientationportrait:yes];
}
-(void) Viewdiddisappear: (BOOL) animated {
[Super viewdiddisappear:animated];
[[Appdelegate delegate] setdeviceorientationportrait:no];
}
Note: This can only be set to the interface supported by the direction of the screen, if the initial access to the interface is the other direction, it can not guarantee the interface to turn back. If the entire project needs to be set up to the screen, it is good to use this method directly.
2, if you just enter the interface is to let the interface in the vertical screen, it is necessary to make a number of replication methods to deal with:
-(BOOL) shouldautorotate {return
YES;
}
-(Uiinterfaceorientationmask) supportedinterfaceorientations {return
uiinterfaceorientationmaskportrait;
}
-(uiinterfaceorientation) preferredinterfaceorientationforpresentation {
return uiinterfaceorientationportrait;
}
This approach needs to be configured as follows, why:
3, the last
Uinavigationcontroller or Uitabbarcontroller, special treatment is required.
Whether to rotate automatically
-(BOOL) shouldautorotate{return
self.topViewController.shouldAutorotate
}
Direction of support
-(uiinterfaceorientationmask) supportedinterfaceorientations{
return self.topViewController.supportedInterfaceOrientations;
}
The first direction is very important
-(uiinterfaceorientation) preferredinterfaceorientationforpresentation{
return self.topViewController.preferredInterfaceOrientationForPresentation;
}
Recommended reading: Https://www.jianshu.com/p/6ac34ab1ea24 selective adoption.