如何在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 关注
- 603 浏览
添加回答
举报
0/150
提交
取消