There are two ways that iOS enforces horizontal screens:
1th: Set the status bar direction, then Vc.view set transform rotation. Note: VC needs to be set to not support horizontal screen.
[[UIApplication sharedapplication] Setstatusbarorientation:uiinterfaceorientationlandscaperight Animated:YES];[UIView animatewithduration:0.25animations:^{Self. View. Transform= Cgaffinetransformmakerotation (m_pi/2);Self. View. Bounds= CGRectMake (0,0, [[UIScreen mainscreen] bounds]. Size. Height, [[UIScreen mainscreen] bounds]. Size. Width);} completion:^ (BOOL finished) {}];
Then rewrite the VC screen rotation method to support only vertical:
#pragma mark //屏幕旋转- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return (toInterfaceOrientation == UIInterfaceOrientationPortrait);}- (BOOL)shouldAutorotate { returnNO;}- (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait;//只支持这一个方向(正常的方向)}
The 2nd kind: Forced horizontal screen VC need to support screen rotation, but only support landscape, other VCs need to set their own screen orientation support.
Forcing a horizontal screen VC is best to use automatic layout, or rewrite viewwilllayoutsubviews and viewdidlayoutsubviews for page layout.
The screen orientation support code for the VC that needs to force the horizontal screen:
#pragma mark 屏幕旋转//iOS5- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft||toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);}//iOS6+- (BOOL)shouldAutorotate { returnYES;}- (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape;}
Other code that does not require a VC that supports horizontal screens (recommended for BASEVC):
#pragma mark //屏幕旋转- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return (toInterfaceOrientation == UIInterfaceOrientationPortrait);}- (BOOL)shouldAutorotate { returnNO;}- (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait;//只支持这一个方向(正常的方向)}
And then it's the most critical place. If you use Navgationcontroller, use its subclasses, and override the screen rotation method in the subclass type:
#pragma mark//Screen rotation- (BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) Tointerfaceorientation {if([ Self. TopviewcontrollerRespondstoselector:@selector(shouldautorotatetointerfaceorientation:)]) {return[ Self. TopviewcontrollerShouldautorotatetointerfaceorientation:tointerfaceorientation]; }return(tointerfaceorientation = = uiinterfaceorientationportrait);} - (BOOL) Shouldautorotate {if([ Self. TopviewcontrollerRespondstoselector:@selector(shouldautorotate)]) {return[ Self. TopviewcontrollerShouldautorotate]; }return NO;} -(Nsuinteger) supportedinterfaceorientations {if([ Self. TopviewcontrollerRespondstoselector:@selector(supportedinterfaceorientations)]) {return[ Self. TopviewcontrollerSupportedinterfaceorientations]; }returnuiinterfaceorientationmaskportrait;//Only support this direction (normal direction)}
================================
Do video playback need to force the horizontal screen, start using the first way to achieve, after the Airplay selection box display is still vertical screen direction, instead of the second way, it is recommended to use the second method.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS Force horizontal screen