handle orientation with multiple stack of viewcontrollers

放肆的年华 提交于 2020-01-25 06:52:21

问题


Some quick issues, i have 5 VC on stack. when i am in my second VC, then if i rotate to landscape and when i move to 1st VC my layout is totally collapsed.

in my second VC i added viewWillTransition and i am invalidateLayoutof my table view. And if i add tableview.reloaddata() in my first screen viewWillAppear .Then it's fine.

But consider if i have 5 VC on stack i can not write code in all VC. Is there any better way to handle it.


回答1:


In your app delegate add this....

    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")
        }
    }

Used this from any view controller like this...

AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo: UIInterfaceOrientation.portrait)


来源:https://stackoverflow.com/questions/57770781/handle-orientation-with-multiple-stack-of-viewcontrollers

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