Swift iOS-如何在设备旋转时隐藏特定选项卡中的选项卡栏,而不是其他选项卡
我的应用程序是基于标签和肖像。在一个特定的视图控制器中,我在场景顶部固定了一个16x9视图,它显示的视频就像YouTube在Swift iOS-如何在设备旋转时隐藏特定选项卡中的选项卡栏,而不是其他选项卡,ios,swift,uiviewcontroller,uitabbarcontroller,device-orientation,Ios,Swift,Uiviewcontroller,Uitabbarcontroller,Device Orientation,我的应用程序是基于标签和肖像。在一个特定的视图控制器中,我在场景顶部固定了一个16x9视图,它显示的视频就像YouTube在方向上所做的那样。同样像YouTube一样,我允许特定的vc旋转到.landscapeRight或.landscapeLeft以全屏模式显示视频。我调用func canRotate()来实现这一点,并在AppDelegate中实现了该功能。其他选项卡或vcs都不能旋转 在vc内部,当设备旋转时,我使用通知来确定方向,因为视频在.landscapeRight或中全屏显示。la
方向上所做的那样。同样像YouTube一样,我允许特定的vc旋转到.landscapeRight
或.landscapeLeft
以全屏模式显示视频。我调用func canRotate()
来实现这一点,并在AppDelegate中实现了该功能。其他选项卡或vcs都不能旋转
在vc内部,当设备旋转时,我使用通知来确定方向,因为视频在.landscapeRight
或中全屏显示。landscapeLeft
我隐藏选项卡栏
我遇到的问题是,当我在一个不同的选项卡上,如tab2或tab3,当我旋转设备时,由于VideoVC内部发生的事情,选项卡栏被隐藏。其他选项卡/VC不旋转,但当处于.landscapeRight
或.landscapeLeft
方向时,选项卡栏消失
当设备旋转时,如何将选项卡栏隐藏在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,但它有问题。它防止了在旋转时将选项卡栏隐藏在VideoVC中,或者如果我在按下通知选项卡之前在VideoVC中旋转了视频,那么它仍然会将选项卡栏隐藏在那里
通知VC:
@objc func deviceOrientationDidChange(){
switch UIDevice.current.orientation {
case .landscapeRight, .landscapeLeft, .portrait:
tabBarController?.tabBar.isHidden = false
defaults: break
}
}
带VideoVC.portrait的tab0(可旋转):
带VideoVC.landscapeLeft的tab0(可旋转):
带NotificationsVC.portrait的选项卡3(无法旋转):
带NotificationsVC.landscapeLeft的tab3(无法旋转,但tabBar是隐藏的,不应该-这就是问题所在):
修复方法很简单(尽管花了几个小时才弄清楚)。我所要做的就是检查view.windownil并将通知选择器中的内容包装在其中
if (self.view.window != nil) {
// put the what's inside the notification's selector in here
}
问题是因为VideoVC和NotificationsVC都是实例化的,我用来检测方向变化的通知是在UIDevice本身上检查VideoVC的视图是否可见。无论我在哪家vc,它都会对设备被告知在方向改变时如何处理tabbar做出响应
NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: .UIDeviceOrientationDidChange, object: nil)
@objc func deviceOrientationDidChange(){
// no matter what vc I was in this was still running
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
}
}
修复方法是将UIDevice.current.orientation开关语句包装在if(self.view.window!=nil){}
中,并且NotifcationsVC不再响应设备方向,因为VideoVC窗口不再可见
代码:
@objc func deviceOrientationDidChange(){
// use this to check if this view's window isn't nil
if (self.view.window != nil) {
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
}
}
}