首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个视图的Swift UIInterfaceOrientation

多个视图的Swift UIInterfaceOrientation
EN

Stack Overflow用户
提问于 2014-09-15 04:18:45
回答 1查看 1.3K关注 0票数 0

我有一个带有多个视图的导航控制器。大多数视图都是纵向视图,因此我将以下代码放在导航视图控制器中,将其锁定到纵向视图中

代码语言:javascript
复制
override func shouldAutorotate() -> Bool {
    return false
}

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

这个效果很好,并且将我所有的视图锁定在纵向。然而,这是一个只需要在Landscape中的视图。如果我使用上面的代码,它会将我的Landscape View锁定为纵向模式,从而切断大部分视图。

有没有人能帮我在景观视图控制器中使用什么来锁定这个仅在景观中的特定视图。

我在用这个,但它不起作用。

代码语言:javascript
复制
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
}
EN

回答 1

Stack Overflow用户

发布于 2015-04-11 01:47:50

对于小型应用程序,我通常使用的解决方案是让导航控制器询问视图本身的旋转首选项:

代码语言:javascript
复制
override func supportedInterfaceOrientations() -> Int {
    return visibleViewController.supportedInterfaceOrientations()
}

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

然后,在每个视图控制器中,您可以覆盖shouldAutorotate和supportedInterfaceOrientations,以便为每个控制器提供所需的行为。

另一个技巧是,为了确保您的视图可以旋转回所需的旋转,您可以使用此技巧有条件地允许旋转回纵向:

代码语言:javascript
复制
override func shouldAutorotate() -> Bool {
    return !UIInterfaceOrientationIsPortrait(self.interfaceOrientation)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25837617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档