简体   繁体   English

多个视图的Swift UIInterfaceOrientation

[英]Swift UIInterfaceOrientation for Multiple views

I have a navigation controller with multiple views. 我有一个具有多个视图的导航控制器。 Most of the views are in portrait, and thus i lock it into portrait view by placing the following code in the Navigation View Controller 大多数视图都是纵向的,因此我通过将以下代码放入“导航视图控制器”中将其锁定为纵向视图

override func shouldAutorotate() -> Bool {
    return false
}

override func supportedInterfaceOrientations() -> Int {
    return UIInterfaceOrientation.Portrait.toRaw()
}

This works just fine, and locks all my views in portrait orientation. 这很好用,并将我所有的视图锁定为纵向。 However, This is 1 view that needs to only be in Landscape. 但是,这是1个视图,只需要位于“景观”中即可。 If I use the code above, it locks my Landscape View, into portrait mode, and thus cutting off most of the view. 如果我使用上面的代码,它将我的风景视图锁定为纵向模式,从而切断了大部分视图。

Can anybody help me out with what to use in the Landscape view controller to lock this particular View in landscape only. 谁能帮助我解决在“景观”视图控制器中使用的功能,以仅将此特定视图锁定在景观中。

I was using this, but it doesn't work. 我正在使用它,但是不起作用。

override func supportedInterfaceOrientations() -> Int {
    return Int(UIInterfaceOrientationMask.Landscape.toRaw())
}

override func shouldAutorotate() -> Bool{
    // This method is the same for all the three custom ViewController
    return true
}

For a small app, the solution I generally use is to have the navigation controller ask the view itself for it's rotation preference: 对于小型应用程序,我通常使用的解决方案是让导航控制器向视图本身询问其旋转首选项:

override func supportedInterfaceOrientations() -> Int {
    return visibleViewController.supportedInterfaceOrientations()
}

override func shouldAutorotate() -> Bool {
    return visibleViewController.shouldAutorotate()
}

Then, in each of your view controllers, you can override shouldAutorotate and and supportedInterfaceOrientations to give each controller the behavior you want. 然后,在每个视图控制器中,您可以覆盖shouldAutorotate和supportedInterfaceOrientations,以为每个控制器提供所需的行为。

One additional trick, to ensure that your view can rotate back to it's desired rotation, you can use this to to conditionally allow rotation back to portrait: 为了确保视图可以旋转回所需的旋转角度,另一种技巧是,可以使用此方法有条件地允许旋转回纵向:

override func shouldAutorotate() -> Bool {
    return !UIInterfaceOrientationIsPortrait(self.interfaceOrientation)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM