美文网首页
下拉时隐藏导航栏

下拉时隐藏导航栏

作者: lifeLL | 来源:发表于2017-01-03 20:37 被阅读0次

方案一:self.navigationController.hidesBarsOnSwipe = YES;

方案二:http://codecloud.net/15369.html

========.h

@interface NavBarViewController : UIViewController

- (void)followSwipeScrollView:(UIView *)scrollView;

@end

========.m

#define NavBarFrame self.navigationController.navigationBar.frame@interface NavBarViewController ()@property (weak, nonatomic)  UIView *scrollView;

@property (strong, nonatomic) UIPanGestureRecognizer *panGesture;

@property (strong, nonatomic) UIView *overLay;

@property (assign, nonatomic) BOOL isHidden;

@end

@implementation NavBarViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

//设置navigationBar跟随滚动的视图(可以是scrollView或者tableview,webview。)

-(void)followSwipeScrollView:(UIView *)scrollView

{

self.scrollView = scrollView;

self.panGesture = [[UIPanGestureRecognizer alloc] init];

self.panGesture.delegate = self;

self.panGesture.minimumNumberOfTouches = 1;

[self.panGesture addTarget:self action:@selector(handlePanGesture:)];

[self.scrollView addGestureRecognizer:self.panGesture];

self.overLay = [[UIView alloc] initWithFrame:self.navigationController.navigationBar.bounds];

self.overLay.alpha = 0;

self.overLay.backgroundColor = self.navigationController.navigationBar.barTintColor;

[self.navigationController.navigationBar addSubview:self.overLay];

[self.navigationController.navigationBar bringSubviewToFront:self.overLay];

}

#pragma mark - 兼容其他手势

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

return YES;

}

#pragma mark - 手势监听方法

-(void)handlePanGesture:(UIPanGestureRecognizer *)panGesture

{

CGPoint translation = [panGesture translationInView:[self.scrollView superview]];

//显示

if (translation.y >= 5) {

if (self.isHidden) {

self.overLay.alpha = 0;

CGRect navBarFrame = NavBarFrame;

CGRect scrollViewFrame = self.scrollView.frame;

navBarFrame.origin.y = 20;

scrollViewFrame.origin.y += 44;

scrollViewFrame.size.height -= 44;

[UIView animateWithDuration:0.5 animations:^{

NavBarFrame = navBarFrame;

self.scrollView.frame = scrollViewFrame;

if ([self.scrollView isKindOfClass:[UIScrollView class]]) {

UIScrollView *scrollView = (UIScrollView *)self.scrollView;

scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y + 44);

}else if ([self.scrollView isKindOfClass:[UIWebView class]]){

UIWebView *webView=(UIWebView *)self.scrollView;

webView.scrollView.contentOffset = CGPointMake(webView.scrollView.contentOffset.x, webView.scrollView.contentOffset.y + 44);

}

}];

self.isHidden = NO;

}

}

//隐藏

if (translation.y <= -20) {

if (!self.isHidden) {

CGRect frame = NavBarFrame;

CGRect scrollViewFrame = self.scrollView.frame;

frame.origin.y = -24;

scrollViewFrame.origin.y -= 44;

scrollViewFrame.size.height += 44;

[UIView animateWithDuration:0.2 animations:^{

NavBarFrame = frame;

self.scrollView.frame = scrollViewFrame;

if ([self.scrollView isKindOfClass:[UIScrollView class]]) {

UIScrollView *scrollView = (UIScrollView *)self.scrollView;

//contentOffset:scrollview当前显示区域顶点相对于frame顶点的偏移量

scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y-44);

}else if ([self.scrollView isKindOfClass:[UIWebView class]]){

UIWebView *webView=(UIWebView *)self.scrollView;

webView.scrollView.contentOffset = CGPointMake(webView.scrollView.contentOffset.x, webView.scrollView.contentOffset.y-44);

}

} completion:^(BOOL finished) {

self.overLay.alpha = 1;

}];

self.isHidden = YES;

}

}

}

-(void)viewDidAppear:(BOOL)animated{

[self.navigationController.navigationBar bringSubviewToFront:self.overLay];

}

@end

相关文章

网友评论

      本文标题:下拉时隐藏导航栏

      本文链接:https://www.haomeiwen.com/subject/zomnvttx.html