简体   繁体   中英

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!

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.

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!

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
    }

}                             

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM