8

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.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
ValentiGoClimb
  • 750
  • 3
  • 13
  • 23
  • I tried with `if ([[[self tabBarController] tabBar] isHidden])` and it's working very well. – Bhavin Aug 13 '13 at 10:51

8 Answers8

6

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
}
Julian
  • 9,299
  • 5
  • 48
  • 65
5

You can try this

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

    NSLog(@"tabBar IS HIDDEN");
}
else
{
    NSLog(@"tabBar IS VISIBLE");
}
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
3

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.

Juraj Antas
  • 3,059
  • 27
  • 37
2

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
}
Federico Zanetello
  • 3,321
  • 1
  • 25
  • 22
1

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
}
juanjo
  • 3,737
  • 3
  • 39
  • 44
1

It seems when you present modally a VC over a TabBarController the tabBar.isHidden property is still false. So adding a bit of backup code for this scenario will do the work.

private func isTabBarVisible() -> Bool {
    var isTabBarVisible = false
    if let tabBarController = navigationController?.tabBarController, tabBarController.tabBar.isHidden {
        isTabBarVisible = true
    }
    if let navigationController = navigationController, navigationController.hidesBottomBarWhenPushed {
        isTabBarVisible = true
    }
    if isModal() {
        isTabBarVisible = true
    }
    print(" tabBar visible: \(isTabBarVisible)")
    return isTabBarVisible
}

private func isModal() -> Bool {
    if presentingViewController != nil { return true }
    if navigationController?.presentingViewController == navigationController { return true }
    if (tabBarController?.presentingViewController as? UITabBarController) != nil { return true }
    return false
}

I've found the way to check if a VC has been presented modally in this post: https://stackoverflow.com/a/23620377/7468486, so my answer is basically a mix of both.

-1

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
}
Gonzo
  • 1,533
  • 16
  • 28
-1

you can check with this

if let tabBarController = self.tabBarController, !tabBarController.hidesBottomBarWhenPushed, !tabBarController.tabBar.isHidden {
  print("tab bar visible")
} else { 
  print("tab bar hidden")
}
Marwan Alqadi
  • 795
  • 8
  • 14