首页 > 解决方案 > 返回 viewController 方向。迅速

问题描述

在应用程序中,我只有纵向。但是一种视图是横向的。

问题是它们都由 NavigationController 连接。当我从视图返回时,从横向到纵向的方向没有改变。

如何修复 NavigationController 的关闭方法?

AppDelegate.swift

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

景观视图.sift

import UIKit

class SchemeView: UIViewController {

    let appDel = UIApplication.shared.delegate as! AppDelegate


    override func viewDidLoad() {
        super.viewDidLoad()

        appDel.myOrientation = .landscape
        UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
    }

    //does not work
    override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {

        appDel.myOrientation = .portrait
        UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")

    }
}

标签: iosswiftiphoneuinavigationcontrollerorientation

解决方案


我使用以下方法为自己决定了一切

override func viewWillDisappear(_ animated: Bool) {
        appDel.myOrientation = .portrait
        UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")

    }

推荐阅读