仅在Swift(框架问题)中锁定并强制将一个视图控制器的方向旋转为横向模式

时间:2019-06-24 06:09:06

标签: ios swift screen-orientation

由于我的应用获得了所有方向的支持。我只想将横向模式锁定到特定的UIViewController。

我用过这个https://stackoverflow.com/a/41811798/10150796

  

在所有方向上都可以正常工作,并且仅将纵向模式锁定为   特定的UIViewController

但是当我通过纵向视图将其用于锁定风景模式时,我的视框不会更新为风景。

这是我的ViewController代码

override func viewWillAppear(_ animated: Bool) {
        AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.landscapeLeft, andRotateTo: UIInterfaceOrientation.landscapeLeft)  
}

override func viewWillDisappear(_ animated: Bool) {
        AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.all)
}

应用代理

struct AppUtility {

        static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {

            if let delegate = UIApplication.shared.delegate as? AppDelegate {
                delegate.orientationLock = orientation
            }
        }

        /// OPTIONAL Added method to adjust lock and rotate to the desired orientation
        static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {

            self.lockOrientation(orientation)

            UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
        }
    }

输出第一次来自任何方向的效果都很好:

enter image description here

输出第二次来自纵向,它看起来像这样:(

enter image description here

如果第二次从横向看相同的东西,那很好

我已尝试在过去两天之内解决此问题,但没有发现什么错误,我也尝试手动更新视图框架,并通过此框架进行了更新,但纵向框架尺寸以外的click事件不起作用:(

有人可以告诉我我错了吗?预先感谢

0 个答案:

没有答案