iOS/Swift - Hide/Show UITabBarController when scrolling down/up

后端 未结 5 510
栀梦
栀梦 2020-12-08 17:36

I\'m quite new to iOS development. Right now i\'m trying to hide my tabbar when I scroll down and when scrolling up the tabbar should appear. I would like to have this anima

5条回答
  •  忘掉有多难
    2020-12-08 18:20

    You can control UITabBar precisly by setting up your class as delegate for scrollView and implementing scrolling in scrollViewDidScroll: method.

    Here is an example how I do it my application. You can probably easily modify that for your needs. Some helper function to get UITabBar included.

    #define LIMIT(__VALUE__, __MIN__, __MAX__) MAX(__MIN__, MIN(__MAX__, __VALUE__))
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        CGFloat scrollOffset = scrollView.contentOffset.y;
        CGFloat scrollDiff = scrollOffset - self.previousScrollViewYOffset;
        CGFloat scrollHeight = scrollView.frame.size.height;
        CGFloat scrollContentSizeHeight = scrollView.contentSize.height + scrollView.contentInset.bottom;
        CGFloat scrollOffsetGlobal = scrollOffset + scrollView.contentInset.top;
        [self updateUITabBarY:[self UITabBarView].frame.origin.y + scrollDiff];
        self.previousScrollViewYOffset = scrollOffset;
    }
    
    - (UITabBar*) UITabBarView
    {
        for(UIView *view in self.tabBarController.view.subviews)
        {
            if([view isKindOfClass:[UITabBar class]])
            {
                return (UITabBar*) view;
            }
        }
    
        return nil;
    }
    
    - (void) updateUITabBarY:(CGFloat) y
    {
        UITabBar* tabBar = [self UITabBarView];
        if(tabBar)
        {
            CGRect frame = tabBar.frame;
            frame.origin.y  = LIMIT(y, [self UITabBarMiny], [self UITabBarMaxY]);
            tabBar.frame = frame;
        }
    }
    
    - (CGFloat) UITabBarMiny
    {
        return [UIScreen mainScreen].bounds.size.height - [self UITabBarView].frame.size.height - [[UIApplication sharedApplication] statusBarFrame].size.height + 20.0f;
    }
    
    - (CGFloat) UITabBarMaxY
    {
        return [UIScreen mainScreen].bounds.size.height;
    }
    

提交回复
热议问题