Zoyt
Zoyt

Reputation: 4967

hidesBarsOnSwipe with UITabBar

I recently switched to using a UITabBarController within my app and was not amused to find I could not make hidesBarsOnSwipe work with it. I use to simply say (within the view controller) hidesBarsOnSwipe = true, but now that does not work. If someone could help me make this work, that would be great.

Thanks!

Upvotes: 4

Views: 4356

Answers (4)

Sateesh Pasala
Sateesh Pasala

Reputation: 968

in swift3

  self.navigationController?.barHideOnSwipeGestureRecognizer.addTarget(self, action: "swipeGestuere")  

declare a variable hidden which helps to get the tab bar back

 func swipeGestuere() {
    if (hidden == true){         
    self.bottomTabBar.isHidden = true
        hidden = false
    }
    else{
        self.bottomTabBar.isHidden = false
        hidden = true
    }

}                             

Upvotes: 0

Pavan Kotesh
Pavan Kotesh

Reputation: 144

You can add action to the hideOnSwipe like below

[self.navigationController.barHideOnSwipeGestureRecognizer addTarget:self action:@selector(swipeGesture:)];

Add what ever code you want in the swipeGesture method. Hide/unhide tab bar.

Upvotes: 3

E. Rivera
E. Rivera

Reputation: 10938

I solved this by resizing the UITabBarController just enough to get the tab bar out of the screen:

- (void)setTabBarHidden:(BOOL)hidden
{
    CGRect frame = self.originalViewFrame;
    if (hidden)
    {
        frame.size.height += self.tabBar.size.height;
    }
    self.view.frame = frame;
}

Then you can add KVO your scroll view:

[scrollView addObserver:self
             forKeyPath:@"contentOffset"
                options:NSKeyValueObservingOptionOld
                context:nil];

And hide/show the tab bar on scroll:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    CGPoint oldOffset = [(NSValue *)change[NSKeyValueChangeOldKey] CGPointValue];

    if (!_hidesBarsOnScroll || _scrollView.contentOffset.y == oldOffset.y)
        return;

    // Show on scroll up
    if (_barsHidden &&
        scrollView.contentOffset.y < oldOffset.y &&
        scrollView.contentOffset.y + scrollView.bounds.size.height < scrollView.contentSize.height) // Skip on bottom
    {
        [self.navigationController setNavigationBarHidden:NO
                                                 animated:YES]; // Also navigation bar!
        [self.tabBarController setTabBarHidden:NO
                                      animated:YES];
        _barsHidden = NO;
    }

    // Hide on scroll down
    if (!_barsHidden &&
        scrollView.contentOffset.y > 0 && // Skip on top
        scrollView.contentOffset.y > oldOffset.y)
    {
        [self.navigationController setNavigationBarHidden:YES
                                                 animated:YES];
        [self.tabBarController setTabBarHidden:YES
                                      animated:YES];
        _barsHidden = YES;
    }
}

You can take a look to this implementation here.

Upvotes: -1

Zoyt
Zoyt

Reputation: 4967

I solved the issue. I had embedded the UITabBarController inside a UINavigationController, which I had put as the root view controller for the window. After I made the root just the tab bar controller, it worked like a charm.

Thanks!

Upvotes: 0

Related Questions