【发布时间】:2018-06-15 20:37:32
【问题描述】:
我的应用是基于标签的,并且只有纵向。在一个特定的视图控制器中,我有一个 16x9 的视图固定在场景的顶部,它显示了 YouTube 在.portrait 方向时所做的视频。也像 YouTube 一样,我允许特定的 vc 旋转到 .landscapeRight 或 .landscapeLeft 以全屏模式显示该视频。我打电话给func canRotate() 来实现这一点,并且我在 AppDelegate 中实现了该功能。其他选项卡或 vcs 都不能旋转。
当设备旋转时,在带有视频的 vc 中,我使用通知来确定方向,并且由于视频在 .landscapeRight 或 .landscapeLeft全屏显示,所以我隐藏了标签栏。
我遇到的问题是,当我在另一个选项卡(例如 tab2 或 tab3)上时,当我旋转设备时,由于 VideoVC 内部发生的事情,tabBar 会被隐藏。其他选项卡/vcs 不旋转,但在 .landscapeRight 或 .landscapeLeft 方向时 tabBar 消失
当设备旋转但不隐藏在其他选项卡中时,如何将 tabBar 隐藏在 VideoVC 中?
在 tab0 里面这是 VideoVC:
NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: .UIDeviceOrientationDidChange, object: nil)
@objc func deviceOrientationDidChange(){
switch UIDevice.current.orientation {
case .landscapeRight:
view.transform = transform.rotated(by: -CGFloat.pi / 2)
tabBarController?.tabBar.isHidden = true
case .landscapeLeft:
view.transform = transform.rotated(by: CGFloat.pi / 2)
tabBarController?.tabBar.isHidden = true
case .portrait:
view.transform = CGAffineTransform.identity
tabBarController?.tabBar.isHidden = false
default: break
}
}
@objc func canRotate(){} // allows this vc to rotate
AppDelegate:
...if (rootViewController.responds(to: Selector(("canRotate")))) {
// Unlock landscape view orientations for this view controller
return .allButUpsideDown;
}
我尝试使用通知来禁用将 tabBar 隐藏在其他选项卡中,这些选项卡具有无法旋转的 vcs 但它很糟糕。它阻止了 tabBar 在旋转时隐藏在 VideoVC 中,或者如果我在按下 Notifications 选项卡之前旋转了 VideoVC 内的视频,那么它仍然会将 tabBar 隐藏在那里。
通知VC:
@objc func deviceOrientationDidChange(){
switch UIDevice.current.orientation {
case .landscapeRight, .landscapeLeft, .portrait:
tabBarController?.tabBar.isHidden = false
defaults: break
}
}
tab0 与 VideoVC .portrait(可旋转):
带有 VideoVC .landscapeLeft 的tab0(可以旋转):
带有 NotificationsVC .portrait 的tab3(不能旋转):
带有 NotificationsVC .landscapeLeft 的 tab3(无法旋转,但 tabBar 已隐藏,不应该 - 这是问题所在):
【问题讨论】:
标签: ios swift uiviewcontroller uitabbarcontroller device-orientation