Hide UITabBar on launch

Is it possible to hide UITabBar on launch?

Accepted Reply

Here is an improved version that solves two problems:


(1) The above version no longer allows a tab bar item to be clicked, as clicking it returns the focus of the tabBar rather than the selectedItem. This is fixed by returning [super preferredFocusedView] instead of self.tabBar in the non first-run case.


(2) This hides the tabBar with alpha at launch so it doesn't flicker in. There's probably a more elegant way to do this.


@interface MWTabBarController ()
@property (nonatomic, assign) BOOL firstTime;
@end
@implementation MWTabBarController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.firstTime = YES;
    self.tabBar.alpha = 0;
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(unAlphaTabBar) userInfo:nil repeats:NO];
}
- (void) unAlphaTabBar
{
    self.tabBar.alpha = 1;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (UIView *)preferredFocusedView {
    if (self.firstTime) {
        self.firstTime = NO;
        return self.selectedViewController.preferredFocusedView;
    }
    else {
        return [super preferredFocusedView];
    }
}

Replies

Yes, this is possible.


UITabBarController will hide the tab bar when focus leaves the tab bar, and show the tab bar when focus moves into the tab bar. By default UITabBarController will initially focus the tab bar. You can change this behavior by subclassing UITabBarController and overriding preferredFocusedView like so:


- (UIView*)preferredFocusedView {

return [[self selectedViewController] preferredFocusedView];

}

Actually turns out this hides initially, but then prevents the tabbar from gaining focus when tapping the menu button, so is there a way to have it hide initially and then return the uitabbarbutton as the preferred focus view after?

You can use a boolean in your custom UITabBarController to know if your launch already happened:

@interface MyCustomTabBarController ()

@property (nonatomic, assign) BOOL firstTime;

@end

@implementation MyCustomTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.firstTime = YES;
}

- (UIView *)preferredFocusedView {
    if (self.firstTime) {
        self.firstTime = NO;
        return self.selectedViewController.preferredFocusedView;
    }
    else {
        return self.tabBar;
    }
}

@end

This works well, however with this in place clicking on tab bar items no longer works to select them (swipe down still works fine.) Seems like maybe the bar isn't getting full focus back. Anyone else encounter this?

Here is an improved version that solves two problems:


(1) The above version no longer allows a tab bar item to be clicked, as clicking it returns the focus of the tabBar rather than the selectedItem. This is fixed by returning [super preferredFocusedView] instead of self.tabBar in the non first-run case.


(2) This hides the tabBar with alpha at launch so it doesn't flicker in. There's probably a more elegant way to do this.


@interface MWTabBarController ()
@property (nonatomic, assign) BOOL firstTime;
@end
@implementation MWTabBarController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.firstTime = YES;
    self.tabBar.alpha = 0;
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(unAlphaTabBar) userInfo:nil repeats:NO];
}
- (void) unAlphaTabBar
{
    self.tabBar.alpha = 1;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (UIView *)preferredFocusedView {
    if (self.firstTime) {
        self.firstTime = NO;
        return self.selectedViewController.preferredFocusedView;
    }
    else {
        return [super preferredFocusedView];
    }
}

Awesome guys thanks so much, it was returning [super preferredFocusedView] was the bit I was looking for, works great