问题描述
App使用TabbarController->NavigationController->ViewController架构,想整体界面默认竖屏,然后一些特定界面可支持横竖屏切换或者强制横屏。
问题出现的环境背景及自己尝试过哪些方法
相关代码
在自定义UINavigationController中
override var shouldAutorotate: Bool {
return self.topViewController?.shouldAutorotate ?? false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return self.topViewController?.supportedInterfaceOrientations ?? .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return self.topViewController?.preferredInterfaceOrientationForPresentation ?? .portrait
}
自定义UITabBarController中:
override var shouldAutorotate: Bool {
return self.selectedViewController?.shouldAutorotate ?? false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return self.selectedViewController?.supportedInterfaceOrientations ?? .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return self.selectedViewController?.preferredInterfaceOrientationForPresentation ?? .portrait
}
然后我现在的做法是,在只希望竖屏的VC中:
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
在只希望横屏的VC中:
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return [.landscapeRight, .landscapeLeft]
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .landscapeRight
}
在横竖屏可切换的VC中:
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .allButUpsideDown
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .landscapeLeft
}
项目的info.plist中都勾选了横竖屏。这样试下来出现了几个bug,其中一个是从竖屏界面push到可横竖屏切换的界面,然后在该界面横屏时侧滑返回,而后再在第一个界面上push,再次侧滑返回时侧滑返回的动画消失,转场效果很突兀。
有没有完美的解决方案啊?重谢!