IOS enables a single page to support vertical and horizontal screen, other pages can only portrait screen

Source: Internet
Author: User

 最近在自己的项目里面  
    • 1
    • 2

The implementation method is as follows:
1 first need to select the supported screen orientation in Xcode

2 in Appdelegate
. h

@property (nonatomic,assign)NSInteger allowRotate; 
    • 1

. m medium

//This method will be called when the device changes in the screen-(Nsuinteger) Application: (UIWindow *)    window{//NSLog (@ "Direction =============%ld", _allowrotate); if (_allowrotate = 1) {return Uiinterfaceorientationmaskall; }else{return (uiinterfaceorientationmaskportrait);}} //returns whether device auto-rotation is supported-(bool) shouldautorotate{if (_allowrotate = = 1) {return yes; } return no;}        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st

3 in a controller that needs to support the screen:

In Viewwillapplear

//在视图出现的时候,将allowRotate改为1,    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;    delegate.allowRotate = 1;
    • 1
    • 2
    • 3

In Viewwilldisappear

//在视图出现的时候,将allowRotate改为0,    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;    delegate.allowRotate = 0;
    • 1
    • 2
    • 3

After writing the above code, you will find some problems: when the horizontal screen page directly click the "Back" button to exit, the page is still a horizontal screen, and we need only one page can be horizontal screen, the test needs to add the following code in the Viewwilldisappear:

    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {        SEL selector = NSSelectorFromString(@"setOrientation:");        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];        [invocation setSelector:selector];        [invocation setTarget:[UIDevice currentDevice]];        int val = UIInterfaceOrientationPortrait;        [invocation setArgument:&val atIndex:2];        [invocation invoke];    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

At this point, you can make the app only have Settings page support screen!

In this case, if the app requires the user to change the UI in a horizontal vertical screen (a different UI for a horizontal screen and a vertical screen), you can do so in the following ways

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{    // do something before rotation    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { 屏幕从竖屏变为横屏时执行 }else{ 屏幕从横屏变为竖屏时执行 } }- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ // do something after rotation}

IOS enables a single page to support vertical and horizontal screen, other pages can only portrait 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.