6 Replies
      Latest reply on Oct 19, 2015 4:03 AM by andrewconlan
      andrewconlan Level 1 Level 1 (0 points)

        Is it possible to hide UITabBar on launch?

        • Re: Hide UITabBar on launch
          pschneider Apple Staff Apple Staff (50 points)

          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];

          }

            • Re: Hide UITabBar on launch
              andrewconlan Level 1 Level 1 (0 points)

              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?

                • Re: Hide UITabBar on launch
                  letatas Level 1 Level 1 (0 points)

                  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
                  
                    • Re: Hide UITabBar on launch
                      zeromhz Level 1 Level 1 (10 points)

                      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?

                        • Re: Hide UITabBar on launch
                          zeromhz Level 1 Level 1 (10 points)

                          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];
                              }
                          }