我现在搜索了一天,但没有找到合适的解决方案。
我的应用程序内部包含一个标签栏控制器和导航控制器。
我只希望所有屏幕都是“纵向”,但只有一个子视图是“横向”。

它可以到达那里,但是当我从子视图返回时,实际上仍然保持横向,即使我只允许.portrait ind控制器,父级也显示为横向。

我已经做了:

extension UITabBarController {
   override open var shouldAutorotate: Bool {
      get {
          if let visibleVC = selectedViewController {
              return visibleVC.shouldAutorotate
          }
          return super.shouldAutorotate
      }
   }

   override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
       get {
           if let visibleVC = selectedViewController {
               return visibleVC.preferredInterfaceOrientationForPresentation
           }
           return super.preferredInterfaceOrientationForPresentation
       }
   }

   override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
      // your custom logic for rotation of selected tab
         get {
               if let visibleVC = selectedViewController {
                   return visibleVC.supportedInterfaceOrientations
               }
               return super.supportedInterfaceOrientations
           }
   }
}

extension UINavigationController {
   override open var shouldAutorotate: Bool {
       get {
           if let visibleVC = visibleViewController {
               return visibleVC.shouldAutorotate
           }
           return super.shouldAutorotate
       }
   }

   override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
       get {
           if let visibleVC = visibleViewController {
               return visibleVC.preferredInterfaceOrientationForPresentation
           }
           return super.preferredInterfaceOrientationForPresentation
       }
   }

   override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{
       get {
           if let visibleVC = visibleViewController {
               return visibleVC.supportedInterfaceOrientations
           }
           return super.supportedInterfaceOrientations
       }
   }
}


并在父控制器中:

  override var shouldAutorotate: Bool{
      return true
   }

   override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
        return .portrait
   }

   override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
      return .portrait
   }


当我将设备转到纵向时,视图将再次以纵向显示,但我希望在孩子横向离开后立即进行,即使我不转动设备也是如此。

有任何想法吗?
谢谢!

最佳答案

在您的Landscape View控制器中添加此代码

override func viewDidLoad() {
    super.viewDidLoad()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.myOrientation = .landscapeRight
}


和ViewWillDisappear添加此

override func viewWillDisappear(_ animated: Bool) {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.myOrientation = .portrait
    UIView.setAnimationsEnabled(false)
    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
    UIView.setAnimationsEnabled(true)
    UIViewController.attemptRotationToDeviceOrientation()
    UIView.setAnimationsEnabled(false)
}


在appDeledate中添加此行

var window: UIWindow?
var myOrientation: UIInterfaceOrientationMask = .portrait

关于ios - 支持横向定向的UiViewController将其他 View 置于横向中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59054817/

10-14 20:45