デバイスの向きに問題があります。 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をサポートしています。
私はすでにこの記事に関連するすべての記事を検索しましたが、何も私の問題を解決しませんでした。