簡體   English   中英

UITabBarController和UINavigationController中特定UIViewController中的設備旋轉

[英]Device rotation in a specific UIViewController within UITabBarController and UINavigationController

基本上,這是我的頁面結構,其中:

TBC = UITabBarController; VC = UIViewController; NVC = UINavigationViewController

                  +--> [NVC] --> [VC] --> ... --> [VC]
 Home             |
 [VC] --> [TBC] --+--> [NVC] --> [VC] --> ... --> [VC]
                  |
                  +--> [NVC] --> [VC] --> ... --> [VC]

我希望允許某些特定的VC在不同方向上查看。 最有可能的是葉節點VC 它可能是用於顯示圖片或其他內容的頁面。

大部分應用應僅以縱向查看。 想想Facebook應用程序,其中大多數核心頁面都以縱向查看,而圖片/視頻可以縱向或橫向查看。

我已經根據這個答案提出了解決方案。

AppDelegate.swift

func application (application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
  return checkOrientation(self.window?.rootViewController)
}

checkOrientation函數:

func checkOrientation (viewController: UIViewController?) -> UIInterfaceOrientationMask {
  if viewController == nil {
    return UIInterfaceOrientationMask.Portrait

  } else if viewController is MyViewController {
    return UIInterfaceOrientationMask.AllButUpsideDown

  } else if viewController is MyTabBarController {
    if let tabBarController = viewController as? MyTabBarController,
           navigationViewControllers = tabBarController.viewControllers as? [MyNavigationController] {
      return checkOrientation(navigationViewControllers[tabBarController.selectedIndex].visibleViewController)
    } else {
      return UIInterfaceOrientationMask.Portrait
    }

  } else {
    return checkOrientation(viewController!.presentedViewController)
  }
}

可以改善使用if/else和遞歸的邏輯,但現在可以使用。

如果必須強制使用上一個/下一個UIViewController的方向,則應將此行添加到當前UIViewControllerviewWillDisappear方法中。 這將強制上一個/下一個視圖

override func viewWillDisappear (animated: Bool) {
  super.viewWillDisappear(animated)
  UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
}

當您具有復雜的視圖層次結構時,事情變得很混亂,例如具有多個導航控制器和/或選項卡視圖控制器。

此實現將其置於各個視圖控制器上,以便在它們希望鎖定方向時進行設置,而不是依靠App Delegate來找到它們。

迅捷3

在AppDelegate中:

/// set orientations you want to be allowed in this property by default
var orientationLock = UIInterfaceOrientationMask.all

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

在其他一些全局struct或helper類中,我在這里創建了AppUtility:

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

}

然后在所需的ViewController中鎖定方向:

 override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    AppUtility.lockOrientation(.portrait)
    // Or to rotate and lock
    // AppUtility.lockOrientation(.portrait, andRotateTo: .portrait)

}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // Don't forget to reset when view is being removed
    AppUtility.lockOrientation(.all)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM