Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 返回导航栏时,锁定视图上的设备方向失败_Ios_Swift_Uideviceorientation - Fatal编程技术网

Ios 返回导航栏时,锁定视图上的设备方向失败

Ios 返回导航栏时,锁定视图上的设备方向失败,ios,swift,uideviceorientation,Ios,Swift,Uideviceorientation,我有一个应用程序,在NavigationController中嵌入了几个VCs 我需要所有的风投都锁定在纵向方向,只有一个在纵向和横向方向 我尝试过不同的方法,这一种对我很有效: extension UINavigationController { public override func supportedInterfaceOrientations() -> Int { return visibleViewController.supportedInterfaceOrientations

我有一个应用程序,在NavigationController中嵌入了几个VCs

我需要所有的风投都锁定在纵向方向,只有一个在纵向和横向方向

我尝试过不同的方法,这一种对我很有效:

extension UINavigationController {
public override func supportedInterfaceOrientations() -> Int {
return visibleViewController.supportedInterfaceOrientations()
}
public override func shouldAutorotate() -> Bool {
return visibleViewController.shouldAutorotate()
}

}
然后我修改每个ViewController,如下所示:

class ViewController: UIViewController {
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}

}
当我打开应用程序,从一个VC转到另一个VC时,一切都很好。应仅为纵向的视图仅为纵向视图。然后我打开未修改的视图,它会按预期更改界面方向

但是然后让我们假设我在横向视图中保留最后一个未修改的视图,然后按下导航栏中的“后退”按钮,转到上一个控制器(该控制器被修改为仅纵向)。VC的布局显示为横向

若我一直按“后退”按钮,所有其他VC的行为就好像他们从来并没有被要求只做肖像一样


我使用了这篇文章,我在显示视图控制器和方向锁定差异方面遇到了问题。我将其添加到“解锁”控制器中:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self willRotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft duration:0];
}

因此,当该控制器即将被移除(按下后退按钮)时,它会发送一条消息,表明它应该返回到横向(无论是向左还是向右,都无所谓,因为前一个控制器将从该方向自动旋转到正确的方向)。这在objective-c中,但我相信swift中也有类似的操作。如果动画的持续时间让事情看起来很奇怪,你可能不得不把它弄得一团糟,但我的动画在即时切换下工作得非常好。

我想我能够复制你描述的场景。不幸的是,我试图强迫它更新的常规方法不起作用,但我确实发现它似乎起了作用。将其放入您在
viewdide出现时返回的视图控制器中:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

这个方法似乎对我有效。不过,这是一个相当艰难的过渡。我希望这有帮助

谢谢!我想把它翻译成swift。当我尝试实现
willRotateToInterfaceOrientation
时,它说“willRotateToInterfaceOrientation(u:duration:)”在iOS版本8.0中被弃用:实现ViewWillTransitionOnSize:而不是带有TransitionOrdinator:的[必须在viewWillTransitionToSize中粘贴一些代码。你知道它在iOS8和Swift中应该是什么样子吗?我不知道。我还没有机会深入研究Swift。我有一堆我支持的遗留代码。我会尝试调用transitiontosize函数,而不是willrotate。我猜ing您可以找到一组op参数来发送,这将满足您的需要。您是对的,过渡非常艰难,但至少它为我完成了任务。非常感谢您花时间回答,Korey。我真的非常感谢!@alahab没问题:)