iOS Force horizontal screen

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.