导航栏滑动显示隐藏

本文介绍了在iOS应用中实现导航栏随UIScrollView滑动而自动隐藏或显示的方法,包括使用系统属性hidesBarsOnSwipe、UIScrollViewDelegate代理方法scrollViewDidScroll及scrollViewWillEndDragging。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、好多App都有上下滑动UIScrollview隐藏或者显示导航栏,在这里我说说我觉得有用的几种方法:

1.iOS8之后系统有一个属性hidesBarsOnSwipe

 Objective-C代码如下

  1. self.navigationController.hidesBarsOnSwipe = YES;  
self.navigationController.hidesBarsOnSwipe = YES;

 swift代码如下

  1. self.navigationController?.hidesBarsOnSwipe = true  
self.navigationController?.hidesBarsOnSwipe = true

当使用以上代码时,可以达到效果

2.使用UIScrollViewDelegate一个代理方法

 Objective-C代码如下

  1. - (void)scrollViewDidScroll:(UIScrollView *)scrollView  
  2. {  
  3.     //scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势  
  4.     UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;  
  5.     //获取到拖拽的速度 >0 向下拖动 <0 向上拖动  
  6.     CGFloat velocity = [pan velocityInView:scrollView].y;  
  7.       
  8.     if (velocity <- 5) {  
  9.         //向上拖动,隐藏导航栏  
  10.         [self.navigationController setNavigationBarHidden:YES animated:YES];  
  11.     }else if (velocity > 5) {  
  12.         //向下拖动,显示导航栏  
  13.         [self.navigationController setNavigationBarHidden:NO animated:YES];  
  14.     }else if(velocity == 0){  
  15.         //停止拖拽  
  16.     }  
  17. }  
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势
    UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;
    //获取到拖拽的速度 >0 向下拖动 <0 向上拖动
    CGFloat velocity = [pan velocityInView:scrollView].y;

    if (velocity <- 5) {
        //向上拖动,隐藏导航栏
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }else if (velocity > 5) {
        //向下拖动,显示导航栏
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }else if(velocity == 0){
        //停止拖拽
    }
}

 swift代码如下

  1. func scrollViewDidScroll(scrollView: UIScrollView) {  
  2.           
  3.         let pan = scrollView.panGestureRecognizer  
  4.         let velocity = pan.velocityInView(scrollView).y  
  5.         if velocity < -5 {  
  6.             self.navigationController?.setNavigationBarHidden(true, animatedtrue)  
  7.         } else if velocity > 5 {  
  8.             self.navigationController?.setNavigationBarHidden(false, animatedtrue)  
  9.         }  
  10.           
  11.     }  
func scrollViewDidScroll(scrollView: UIScrollView) {

        let pan = scrollView.panGestureRecognizer
        let velocity = pan.velocityInView(scrollView).y
        if velocity < -5 {
            self.navigationController?.setNavigationBarHidden(true, animated: true)
        } else if velocity > 5 {
            self.navigationController?.setNavigationBarHidden(false, animated: true)
        }

    }

这种效果最好

3.使用UIScrollViewDelegate另一个代理方法

 Objective-C代码如下

  1. - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset</span>  
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset</span>
  1. {  
  2.     if (velocity.y > 0.0) {  
  3.         [self.navigationController setNavigationBarHidden:YES animated:YES];  
  4.     } else if (velocity.y < 0.0){  
  5.         [self.navigationController setNavigationBarHidden:NO animated:YES];  
  6.     }  
  7. }  
{
    if (velocity.y > 0.0) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    } else if (velocity.y < 0.0){
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}

swift代码如下

  1. func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {  
  2.         if velocity.y > 0 {  
  3.             self.navigationController?.setNavigationBarHidden(true, animatedtrue)  
  4.         } else if velocity.y < 0 {  
  5.             self.navigationController?.setNavigationBarHidden(false, animatedtrue)  
  6.         }  
  7.     }  
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        if velocity.y > 0 {
            self.navigationController?.setNavigationBarHidden(true, animated: true)
        } else if velocity.y < 0 {
            self.navigationController?.setNavigationBarHidden(false, animated: true)
        }
    }

转自:http://blog.youkuaiyun.com/wgl_happy/article/details/51791937

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值