iOS代碼處理橫屏問題

來源:互聯網
上載者:User

標籤:

藉助通知來控制介面的橫豎屏切換。
還是整個App中大部分介面都是豎屏,某個介面可以橫豎屏切換的情況。

首先,在【General】-->【Device Orientation】設定僅支援豎屏,like this:

Device Orientation

然後在特殊的視圖控制器裡的ViewDidLoad中註冊通知:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];

通知方法的實現過程:

- (void)deviceOrientationDidChange{    NSLog(@"deviceOrientationDidChange:%ld",(long)[UIDevice currentDevice].orientation);    if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];        [self orientationChange:NO];        //注意: UIDeviceOrientationLandscapeLeft 與 UIInterfaceOrientationLandscapeRight    } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];        [self orientationChange:YES];    }}- (void)orientationChange:(BOOL)landscapeRight{    if (landscapeRight) {        [UIView animateWithDuration:0.2f animations:^{            self.view.transform = CGAffineTransformMakeRotation(M_PI_2);            self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);        }];    } else {        [UIView animateWithDuration:0.2f animations:^{            self.view.transform = CGAffineTransformMakeRotation(0);            self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);        }];    }}// 用到的兩個宏:    #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)    #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

最重要的一點:
需要重寫如下方法,並且返回NO。

- (BOOL)shouldAutorotate{    return NO;}

這樣,在裝置出於橫屏時,介面就會變成橫屏,裝置處於豎屏時,介面就會變成豎屏。

填坑
  • 上面方式二,因為【General】-->【Device Orientation】因為只設定了豎屏,所以當橫屏時,如果有鍵盤彈出,鍵盤是豎屏時的樣式。
    解決辦法:在【General】-->【Device Orientation】中加上橫屏時的方向。

  • 如果VieController 是放在UINavigationController或者UITabBarController中,需要重寫它們的方向控制方法。

// UINavigationController:- (BOOL)shouldAutorotate{    return [self.topViewController shouldAutorotate];}- (UIInterfaceOrientationMask)supportedInterfaceOrientations{    return [self.topViewController supportedInterfaceOrientations];}// UITabBarController:- (BOOL)shouldAutorotate{    return [self.selectedViewController shouldAutorotate];}- (UIInterfaceOrientationMask)supportedInterfaceOrientations{    return [self.selectedViewController supportedInterfaceOrientations];}
  • 如果想要點擊某個按鈕之後,強制將豎屏顯示的介面變成橫屏呢?
    有人可能會想到這樣寫:

// 橫屏- (IBAction)landscapAction:(id)sender {    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];    [self orientationChange:YES];}

但是按照上面的寫法,會導致返回到之前的介面時,視圖方向錯誤,即使返回前執行如下代碼:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];[self orientationChange:NO];

也沒有作用,下面是在開源工程中無意看到的寫法:

// 橫屏- (IBAction)landscapAction:(id)sender {    [self interfaceOrientation:UIInterfaceOrientationLandscapeRight];}// 豎屏- (IBAction)portraitAction:(id)sender {    [self interfaceOrientation:UIInterfaceOrientationPortrait];}- (void)interfaceOrientation:(UIInterfaceOrientation)orientation{    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                  = orientation;        [invocation setArgument:&val atIndex:2];        [invocation invoke];    }}

上面的方法會將裝置的方向強制設定為某個方向,然後再監控裝置方向改變的通知,即可實現橫豎屏切換。
這裡有一個用JS 和原生item 控制橫豎屏切換的Demo。地址
這是:

iOS代碼處理橫屏問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.