2017-12-17 9 views
-1

デバイスの向きに問題があります。 UITabBarControllerがあり、LandscapeRightにする必要がある1つのタブ以外のすべてのタブがポートレートになる必要があります。 は、私はそれが部分的に動作しますが、物事が厄介行くこのシナリオを考えるようになった:プログラムで設定したときに方向が変わってきます

目標UITabBarのタブを変更するボタンを使用して肖像画から風景への変更(

作品:キープします画面を物理的にポートレートで表示し、タブを変更するボタンを押します。

:画面が物理的に横長に維持されますe肖像画スクリーン。次に、タブを変更するボタンをタップすると、画面が回転しないという結果になります。

私の問題を理解している場合、デバイスが物理的にどの方向にあるか(ユーザーがボタンを押す前にそれをどのように保持しているかによって)結果が異なります。

これらは私が状況を処理するために使用しています関連するコードスニペットです:UITabBarController

:私はこのコードを持っている肖像画のビューコントローラで

extension UITabBarController { 

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

} 
func landscapeSelected() { 
    AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.landscapeRight, andRotateTo: UIInterfaceOrientation.landscapeRight) 

} 
override open var shouldAutorotate: Bool { 
    get { 
     return true 
    } 
} 


override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{ 
    get { 
      return self.selectedViewController?.supportedInterfaceOrientations ?? .portrait 

     } 
    } 
} 

override var supportedInterfaceOrientations: UIInterfaceOrientationMask { 
    return .portrait 
} 

override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 
     if let tabBar = self.tabBarController { 
      tabBar.portraitSelected() 
     } 
    } 
風景ビューコントローラで

override var supportedInterfaceOrientations: 
    UIInterfaceOrientationMask { 
     return .landscapeRight 
    } 

override func viewDidAppear(_ animated: Bool) { 
super.viewDidAppear(animated) 
if let tabBar = self.tabBarController { 
    tabBar.landscapeSelected() 
} 

}

最後に、AppDelegateでIは、(上記のコードで呼ばれている)これらの静的関数

を添加
var orientationLock = UIInterfaceOrientationMask.portrait 

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) 
     if rotateOrientation.isPortrait { 
      print("portrait") 
     } else if rotateOrientation.isLandscape { 
      print("landscape") 
     } 
     UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation") 
    } 
} 

ビルドの設定に私はportraiを設定しましたtとlandscapeRightをサポートしています。

私はすでにこの記事に関連するすべての記事を検索しましたが、何も私の問題を解決しませんでした。

答えて

0

最終的に私はUITabBarControllerの外の風景UIViewControllerを削除してこの問題を解決しました。

@IBAction func landscapeTapped(_ sender: Any) { 
    let story = UIStoryboard.init(name: "Main", bundle: nil) 
    let land = story.instantiateViewController(withIdentifier: "landscape") as! LanscapeViewController 
    let window = UIApplication.shared.delegate as! AppDelegate 
    window.window?.rootViewController = land 
} 

、これはオリエンテーションの問題を回避: 風景/ポートレートがタップされたとき、私は今、ちょうど前後にアプリのルートビューコントローラを変更しています。

関連する問題