Check if tabBar is visible on iOS app

大城市里の小女人 提交于 2019-12-18 16:11:51

问题


I am working on an iOS App that have an UITabBarController for show a TabBar. In some where, I present a modalView full screen that hides the tabBar.

I want to detect when my tabBar is visible for the user. There is any way to check automatically when de tabBar is visible or not?

I tried that:

But it really don't work because the tabBar is not really hidden.

if ([[[appdelegate tabBarController] tabBar] isHidden])
{
    NSLog(@"tabBar IS HIDDEN");
}
else
{
    NSLog(@"tabBar IS VISIBLE");
}

I write this code in a BaseViewController which is super class of my modal view and the other views of my project.

Thanks.


回答1:


Checking this [[[self tabBarController] tabBar] isHidden] is fine but in one case it will fail. If you do not have tab bar in that view (at all) then [self tabBarController] returns nil so calling isHidden will return NO which is the truth but you have to detect that situation that it is other case. It is not hidden but it doesn't exits so, except that checking you should add [self tabBarController] != nil. So basically:

if([self tabBarController] && ![[[self tabBarController] tabBar] isHidden]){
    //is visible
} else {
    //is not visible or do not exists so is not visible
}



回答2:


You can try this

if ([[[self tabBarController] tabBar] isHidden]){

    NSLog(@"tabBar IS HIDDEN");
}
else
{
    NSLog(@"tabBar IS VISIBLE");
}



回答3:


I use this in Swift:

tabBarController?.tabBar.isHidden ?? true

I use it to find the tab bar height:

var tabBarHeight: CGFloat {
    if tabBarController?.tabBar.isHidden ?? true { return 0 }
    return tabBarController?.tabBar.bounds.size.height ?? 0
}



回答4:


Answer in Swift 3/4+

if
  let tabBarController = self.tabBarController,
  !tabBarController.tabBar.isHidden {
  // tabBar is visible
} else {
  // tabBar either is not visible or does not exist
}



回答5:


This is probably easiest way: (assuming you are not playing directly with views)

ViewController that is going to be pushed to navigationController has a property hidesBottomBarWhenPushed. Just check if it is YES in the view controller and you know if tabbar was hidden or not.




回答6:


you can check with this

if let tabBarController = self.tabBarController, !tabBarController.hidesBottomBarWhenPushed, !tabBarController.tabBar.isHidden {
  print("tab bar visible")
} else { 
  print("tab bar hidden")
}



回答7:


Check the window property of tabBar. This property is set to nil whent it's UIView is not visible.

if((BOOL)[[[self tabBarController] tabBar] window])
{
    // visible
}
else
{
    // not visible
}


来源:https://stackoverflow.com/questions/18206838/check-if-tabbar-is-visible-on-ios-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!