天天看點

iOS 橫豎屏的切換以及個别界面橫屏

進入正文前先認識幾個概念

     portrait  豎屏(Home鍵在下邊)

     upside down  豎屏(Home鍵在上邊)

     landscape  橫屏   |landscape left  橫屏Home鍵在左邊

                                  |landscape right 橫屏Home鍵在右邊

1、先讓視窗支援橫豎屏

     兩種方法可以修改視窗對橫豎屏的支援

     一種,代碼控制

      在appDelegate中重寫方法,比如

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

    if (self.isShouAutoRotate) {

        return UIInterfaceOrientationMaskAll;

    }

    return UIInterfaceOrientationMaskPortrait;

}

      另一種就是,在【General】-->【Device Orientation】中設定好支援的方向

      如圖,,,,,,

     兩種方法的利弊,讀者自己推敲。

2、下面認識三個方法

// 是否自動旋轉

- (BOOL)shouldAutorotate {

    return YES;

}

// 傳回支援的方向

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskLandscapeRight;

}

// 切換橫豎屏時,可以重寫這個方法,來重新布局界面

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {}

3、個别界面橫屏

     第一種處理方式:   1中對應的設定支援橫屏完成後,在相應的控制器直接實作2中的方法二

     第二種處理方式:   1中對應的設定支援橫屏完成後,在相應控制器的viewDidLoad方法中強制設定裝置朝向,代碼如下:

    NSNumber *value = [NSNumber numberWithInt:4];

    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];

看完是不是很想吐槽,來吧,評論等着你