iOS開發--屏幕旋轉的問題

首先,在iOS中展示界面的方式有兩種,push和modal
那么,在app中如果想要實現隨心所欲的屏幕旋轉,就要分兩種情況來看
1,使用push,也就是說我們的控制器要嵌套在一個UINavigationController下來展示,我們需要以個繼承了UINavigationController的子類來重寫一下三個方法

- (BOOL)shouldAutorotate{
    return self.topViewController.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return self.topViewController.supportedInterfaceOrientations;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return self.topViewController.preferredInterfaceOrientationForPresentation;
}

然后在相應的控制器里也要重寫著三個方法,例如這樣

- (BOOL)shouldAutorotate{
    return self.autoRotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return self.mask;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return self.orientation;
}

這里重寫的目的是為了真正控制當前的屏幕方向
重寫這三個方法也是做一個鋪墊,真正發揮改變屏幕方向的代碼是

    [[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeLeft) forKey:@"orientation"];

只有重寫了前面的三個方法,上面的代碼才會起作用
以上的步驟的確可以實現屏幕旋轉,但是無論是modal還是push 我都發現還存在問題,modal 的情況下會偶發控制器的view 旋轉了但是屏幕的方向沒有變,push 的問題發生打概率更大一些,就是當從A到B變橫屏,然后從B到C變豎屏,再從C返回B能夠變回橫屏,然后從B返回A也能變回豎屏,這時候再從Apush到B則不能變橫屏,這是目前遇到的問題,記錄下來,暫時留在這里,明天繼續解決。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容