在iOS 8之前,我们将以下代码与supportedInterfaceOrientations和shouldAutoRotate委托方法结合使用,以强制应用程序定向到任何特定的定向。我使用下面的代码片段以编程方式将应用程序旋转到所需的方向。首先,我正在更改状态栏的方向。然后,只需显示并立即关闭模式视图,就可以将视图旋转到所需的方向。
[[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开始应该始终避免这种方法。
更具体地说,我的应用程序是一个通用类型的应用程序。总共有三个控制器。
我们正在使用导航控制器进行页面导航。从第一个视图控制器,在一个按钮单击动作上,我们将第二个推入堆栈。因此,当第二个视图控制器到达时,无论设备方向如何,应用程序应该只锁定在横向方向。
下面是我在第二个和第三个视图控制器中的shouldAutorotate
和supportedInterfaceOrientations
方法。
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeRight;
}
-(BOOL)shouldAutorotate {
return NO;
}
在iOS 8的特定方向上,有没有更好的锁定视图控制器的方法。请帮助!!
发布于 2014-10-14 18:11:44
对于iOS 7- 10:
Objective-C:
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
[UINavigationController attemptRotationToDeviceOrientation];
Swift 3:
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
只需在所呈现的视图控制器的- viewDidAppear:
中调用它。
发布于 2015-01-30 01:13:03
如果您在UINavigationController
或UITabBarController
中,方向旋转会稍微复杂一些。问题是,如果视图控制器嵌入到其中一个控制器中,则导航或选项卡栏控制器优先,并决定自动旋转和支持的方向。
我在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
,否则,您可以省略其他视图控制器中的重写,以便继承应用程序plist中指定的默认方向行为
禁用旋转
class ViewController: UIViewController {
override func shouldAutorotate() -> Bool {
return false
}
}
锁定到特定方向
class ViewController: UIViewController {
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Landscape.rawValue)
}
}
从理论上讲,这应该适用于所有复杂的视图控制器层次结构,但我注意到UITabBarController有一个问题。由于某些原因,它想要使用默认方向值。如果您有兴趣了解如何解决某些问题,请参阅以下博客文章:
发布于 2015-03-19 00:52:40
这是对我有效的方法:
在你的viewDidAppear:方法中调用它。
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[UIViewController attemptRotationToDeviceOrientation];
}
https://stackoverflow.com/questions/26357162
复制相似问题