导航栏滑动显示隐藏

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

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

 Objective-C代码如下

[objc] view plain copy
print ?
  1. self.navigationController.hidesBarsOnSwipe = YES;  
self.navigationController.hidesBarsOnSwipe = YES;

 swift代码如下

[objc] view plain copy
print ?
  1. self.navigationController?.hidesBarsOnSwipe = true  
self.navigationController?.hidesBarsOnSwipe = true

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

2.使用UIScrollViewDelegate一个代理方法

 Objective-C代码如下

[objc] view plain copy
print ?
  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代码如下

[objc] view plain copy
print ?
  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代码如下

[objc] view plain copy
print ?
  1. - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset  
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
[objc] view plain copy
print ?
  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代码如下

[objc] view plain copy
print ?
  1. func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) {  
  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) {
        if velocity.y > 0 {
            self.navigationController?.setNavigationBarHidden(true, animated: true)
        } else if velocity.y < 0 {
            self.navigationController?.setNavigationBarHidden(false, animated: true)
        }
    }

转自:http://blog.csdn.net/wgl_happy/article/details/51791937

你可能感兴趣的:(导航栏滑动显示隐藏)