overriding shouldAutorotate not working in Swift 3

柔情痞子 提交于 2019-12-18 01:31:12

问题


I'm trying to prevent rotation on one UIViewController and I can't achieve that.

I'm doing something like this:

open override var shouldAutorotate: Bool {
    get {
        return false
    }
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    get {
        return .portrait
    }
}

And the UIViewControler stills rotating. The UIViewController is inside a UINavigationController opened modally.

I have looked a lot of questions from here and none answers works for me.

In Swift 2 I used to override shouldAutorotate but in Swift 3 that function doesn't exist anymore.

How can I do that in Swift 3 what I used to do in Swift 2?


回答1:


I don't know why is a vote to close the question if I can reproduce this behavior a lots of times. The UIViewController is inside a UINavigationController opened modally.

This is what I did to solve the problem.

I create this class and I set to the UINavigationController that containt the UIViewController that I want to prevent to rotate

class NavigationController: UINavigationController { 

    override var shouldAutorotate: Bool {
        return false
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }

}

And thats it, it works for me




回答2:


Add this code to AppDelegate.swift

var orientationLock = UIInterfaceOrientationMask.all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return self.orientationLock
}

struct AppUtility {
    static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
        if let delegate = UIApplication.shared.delegate as? AppDelegate {
            delegate.orientationLock = orientation
        }
    }

    static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
        self.lockOrientation(orientation)
        UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
    }
}

Add to the viewcontroller that you want to force an orientation:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    //let value = UIInterfaceOrientation.landscapeLeft.rawValue
    //UIDevice.current.setValue(value, forKey: "orientation")


    AppDelegate.AppUtility.lockOrientation(.landscapeLeft)

}



回答3:


For me, the voted answer didn't work. Instead,

override open var shouldAutorotate: Bool {
    return false
}

override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return UIApplication.shared.statusBarOrientation
}

Those codes work. Confirmed in Swift 4.




回答4:


I found that iOS 11 wasn't calling these methods at all unless I had implemented the following UIApplicationDelegate method in my AppDelegate class:

application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?)



回答5:


iOS 11, Swift 5

The new secret sauce is to add this in viewDidLoad()

UIDevice.current.setValue(self.preferredInterfaceOrientationForPresentation.rawValue, forKey: "orientation")

Also be sure to override preferredInterfaceOrientationForPresentation to return your preferred orientation:

override var preferredInterfaceOrientationForPresentation : UIInterfaceOrientation { return UIInterfaceOrientation.portrait }

Found the answer here: https://nunoalexandre.com/2018/08/24/forcing-an-orientation-in-ios



来源:https://stackoverflow.com/questions/40413567/overriding-shouldautorotate-not-working-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!