如何在CollectionView Swift上禁用景观



我的应用程序同时支持景观和肖像,但我想在我的一个收集视图和ViewControllers上禁用景观旋转。我的代码在ViewController上工作,但在CollectionView中不起作用。有什么建议吗?

    override var shouldAutorotate: Bool{
    return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

我总是使用此

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

另外,没有旋转UICollectionView之类的东西,它是您的ViewController

就像在汽车中一样,驾驶员处理整个汽车

编辑:

它可能是正确的代码,但不在正确的ViewController中。为了 例如,如果视图控制器嵌入 uinavigation controller,导航控制器仍然可以旋转, 导致视图控制器仍旋转。这确实取决于你 特定情况。

假设您的ViewController已嵌入到Navigation Controller中,那么您最好的选择是将Uinavigation Controller

subsclass
import UIKit    
class CustomNavigationController: UINavigationController {
    override func shouldAutorotate() -> Bool {
        if !viewControllers.isEmpty {
            // Check if this ViewController is the one you want to disable roration on
            if topViewController!.isKindOf(ViewController) {               //ViewController is the name of the topmost viewcontroller
                // If true return false to disable it
                return false
            }
        }
        // Else normal rotation enabled
        return true
       }
    }

如果要在整个导航控制器中禁用自动旋转

最新更新