ios,scrollview 或 tableview中实现上滚隐藏tabbar,下滚出现tabbar

来源:互联网 发布:南华大学船山学院 知乎 编辑:程序博客网 时间:2024/06/05 06:19

重载delegate方法:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    static float mark1 = 0;      //静态变量,记录contentOffset上次的位置
    float offY = scrollView.contentOffset.y;   //本次contentOffset的位置
    if (offY > mark1) {      //offY > mark1 表示现在是下滚状态
        if (offY > 0) {        // 这个判断很关键,不信去掉试试
            [self hideTabBar:YES];   //隐藏tabbar
        }
        
    }
    else              //offY < mark1 表示现在是上滚状态
    {
        if (offY < scrollView.contentSize.height - scrollView.frame.size.height) {  // 这个判断很关键,不信去掉试试
            [self hideTabBar:NO];   //不隐藏tabbar
        }
        
    }
    mark1 = offY;    ///静态变量记录contentOffset本次的位置
}


- (void) hideTabBar:(BOOL) hidden{     //自写方法用于隐藏tabbar
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];    //设置隐藏和非隐藏的时间间隔
    
    for(UIView *view in self.tabBarController.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]] )
        {
            if (hidden) {
                [view setFrame:CGRectMake(view.frame.origin.x, iphone5?568:480, view.frame.size.width, view.frame.size.height)];
            } else {
                [view setFrame:CGRectMake(view.frame.origin.x, iphone5?568-49:480-49, view.frame.size.width, view.frame.size.height)];
            }
        }
        else
        {
            if (hidden) {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, iphone5?568:480)];
            } else {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width,  iphone5?568-49:480-49)];
            }
        }
    }
    
    [UIView commitAnimations];
}


思路出来了,方法就很简单,大家都容易看懂,就不说了,有不懂的请在下面回复。

【原创文章,欢迎转载,转载请注明出处,谢谢!】