简体   繁体   English

将肖像方向限制为iOS中的collectionview似乎不起作用

[英]Restricting portrait orientation to collectionview in iOS doesn't seem to work

I have multiple viewControllers in my app and I would like to restrict certain views only to portrait orientation. 我的应用程序中有多个viewControllers,我想仅将某些视图限制为纵向。 I have achieved this by overriding shouldAutoRotate and supportedInterfaceOrientations like below 我已经通过覆盖shouldAutoRotate和supportedInterfaceOrientations如下实现

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

    override func supportedInterfaceOrientations() -> Int {

        return Int(UIInterfaceOrientation.Portrait.rawValue)
    }

I have followed the same for a UICollectionView but it doesn't seem to work. 我为UICollectionView遵循了相同的方法,但是似乎没有用。 I would like to know if this is the right way to achieve it. 我想知道这是否是实现它的正确方法。 Help on this would be much appreciated. 帮助将不胜感激。 Thanks in advance! 提前致谢!

In iOS 8 or later, if you want to control the allowed orientations per view controller: 在iOS 8或更高版本中,如果要控制每个视图控制器的允许方向:

  1. Make sure the "Supported interface orientations" in the Info.plist allows all orientations you want the app to be able to use. 确保Info.plist的“支持的界面方向”允许您希望应用程序能够使用的所有方向。 (Or just check the correct "Device Orientations" in the Xcode UI interface to the Info.plist file) (或者只是在Xcode UI界面中检查Info.plist文件的正确“设备方向”)

  2. Per view controller, implement (example restricting to portrait): 对于每个视图控制器,实施(示例仅限于肖像):

     override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.Portrait } 

A problem you can run in to is if your view controller is embedded in another view controller (like an UINavigationController or a UITabBarController, or both). 您可能遇到的问题是,如果您的视图控制器嵌入在另一个视图控制器中(例如,UINavigationController或UITabBarController,或两者都嵌入)。

I suspect this is the situation you are describing. 我怀疑这就是您要描述的情况。

If so, all view controllers involved need to implement the supportedInterfaceOrientations() method. 如果是这样,则所有涉及的视图控制器都需要实现supportedInterfaceOrientations()方法。 AFAIK, if you for example have a UINavigationController above your view controller, you need to sub class and create your own navigation controller with the method implemented. AFAIK,例如,如果您的视图控制器上方有一个UINavigationController,则需要子类化并使用实现的方法创建自己的导航控制器。

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

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