如何在IOS 8中強制查看控制器方向?在iOS 8之前,我們將下面的代碼與支持接口定向和應自旋委托方法強制應用程序定向到任何特定方向。我使用下面的代碼片段以編程方式將應用程序旋轉到所需的方向。首先,我改變了狀態欄的方向。然后,只要呈現并立即丟棄一個模態視圖,就可以將視圖旋轉到所需的方向。[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
UIViewController *c = [[UIViewController alloc]init];[self presentViewController:vc animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];但這在iOS 8中是失敗的。而且,我在堆棧溢出中看到了一些答案,人們建議我們應該從iOS 8開始始終避免這種方法。更具體地說,我的應用程序是一種通用的應用程序類型??偣灿腥齻€控制器。第一視圖控制器-它應該支持iPad中的所有方向,只支持iPhone中的肖像畫(主頁按鈕)。第二視圖控制器-在任何情況下都應支持景觀權第三視圖控制器-在任何情況下都應支持景觀權我們使用導航控制器進行頁面導航。從第一個視圖控制器,在一個按鈕單擊操作,我們推動第二個在堆棧上。因此,當第二個視圖控制器到達時,不管設備的方向如何,應用程序應該只鎖定在景觀右側。下面是我的shouldAutorotate和supportedInterfaceOrientations方法在第二視圖控制器和第三視圖控制器中。-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeRight;}-(BOOL)shouldAutorotate {
return NO;}是否有任何解決方案或任何更好的方法鎖定一個視圖控制器,特別是iOS 8的方向。請幫助!
3 回答

阿波羅的戰車
TA貢獻1862條經驗 獲得超6個贊
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"]; [UINavigationController attemptRotationToDeviceOrientation];
let value = UIInterfaceOrientation.landscapeLeft.rawValueUIDevice.current.setValue(value, forKey: "orientation") UINavigationController.attemptRotationToDeviceOrientation()
- viewDidAppear:

溫溫醬
TA貢獻1752條經驗 獲得超4個贊
UINavigationController
UITabBarController
給視圖控制器權力!
SWIFT 2.3
extension UINavigationController { public override func supportedInterfaceOrientations() -> Int { return visibleViewController.supportedInterfaceOrientations() } public override func shouldAutorotate() -> Bool { return visibleViewController.shouldAutorotate() }}extension UITabBarController { public override func supportedInterfaceOrientations() -> Int { if let selected = selectedViewController { return selected.supportedInterfaceOrientations() } return super.supportedInterfaceOrientations() } public override func shouldAutorotate() -> Bool { if let selected = selectedViewController { return selected.shouldAutorotate() } return super.shouldAutorotate() }}
SWIFT 3
extension UINavigationController { open override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return visibleViewController?.supportedInterfaceOrientations ?? super.supportedInterfaceOrientations } open override var shouldAutorotate: Bool { return visibleViewController?.shouldAutorotate ?? super.shouldAutorotate }}extension UITabBarController { open override var supportedInterfaceOrientations: UIInterfaceOrientationMask { if let selected = selectedViewController { return selected.supportedInterfaceOrientations } return super.supportedInterfaceOrientations } open override var shouldAutorotate: Bool { if let selected = selectedViewController { return selected.shouldAutorotate } return super.shouldAutorotate }}
supportedInterfaceOrientations
shouldAutoRotate
禁用旋轉
class ViewController: UIViewController { override func shouldAutorotate() -> Bool { return false }}
鎖定到特定方向
class ViewController: UIViewController { override func supportedInterfaceOrientations() -> Int { return Int(UIInterfaceOrientationMask.Landscape.rawValue) }}
- 3 回答
- 0 關注
- 645 瀏覽
添加回答
舉報
0/150
提交
取消