检查tabBar是否在iOS应用程序中可见
我正在iOS应用程序有一个UITabBarController显示一个TabBar。 在某些地方,我提出了一个隐藏TabBar的modalView全屏幕。
我想检测当我的tabBar是可见的用户。 当tabBar可见或不可见时,有什么办法可以自动检查?
我试过了:
但它真的不工作,因为tabBar没有真正隐藏。
if ([[[appdelegate tabBarController] tabBar] isHidden]) { NSLog(@"tabBar IS HIDDEN"); } else { NSLog(@"tabBar IS VISIBLE"); }
我在一个BaseViewController中编写了这个代码,它是我的模态视图和我的项目的其他视图的超类。
谢谢。
检查这[[[self tabBarController] tabBar] isHidden]
是好的,但在一种情况下,它会失败。 如果你没有在该视图中的标签栏(在所有)然后[self tabBarController]
tabBarController [self tabBarController]
返回nil
所以调用isHidden将返回NO这是事实,但你必须检测到的情况,这是其他情况。 它不隐藏,但它不退出,除了检查你应该添加[self tabBarController] != nil
。 所以基本上:
if([self tabBarController] && ![[[self tabBarController] tabBar] isHidden]){ //is visible } else { //is not visible or do not exists so is not visible }
你可以试试这个
if ([[[self tabBarController] tabBar] isHidden]){ NSLog(@"tabBar IS HIDDEN"); } else { NSLog(@"tabBar IS VISIBLE"); }
回答Swift 3/4 +
if let tabBarController = self.tabBarController, !tabBarController.tabBar.isHidden { // tabBar is visible } else { // tabBar either is not visible or does not exist }
这可能是最简单的方法:(假设你不直接玩视图)
ViewController将被推送到navigationController有一个属性hidesBottomBarWhenPushed。 只要检查视图控制器中是否为YES,并知道tabbar是否隐藏。
我在Swift中使用这个:
tabBarController?.tabBar.isHidden ?? true
我用它来查找标签栏高度:
var tabBarHeight: CGFloat { if tabBarController?.tabBar.isHidden ?? true { return 0 } return tabBarController?.tabBar.bounds.size.height ?? 0 }
检查tabBar
的window
属性。 这个属性设置nil
因为它的UIView
是不可见的。
if((BOOL)[[[self tabBarController] tabBar] window]) { // visible } else { // not visible }