1.想实现的效果:
浏览文章的时候,当向下滑动时候,navigationBar 和 toolbar 隐藏 , 当到结尾时候再向上滑动,navigationBar 和 toolbar 重新显示出来。
2.思路:
首先,这里用来显示文章的是webview ,我们都知道webview中包含scrollview,这样就好办了,我们利用scrollview来实现即可。
代码如下:
#pragma mark - UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { int currentPostion = scrollView.contentOffset.y; if (currentPostion - lastPostion > kSlide && currentPostion > 0) { lastPostion = currentPostion; //重设frame [UIView animateWithDuration:kAnimationTime animations:^{ CGRect rc = self.navigationController.navigationBar.frame; self.navigationController.navigationBar.frame = CGRectMake(0, -CGRectGetHeight(rc), CGRectGetWidth(rc), CGRectGetHeight(rc)); rc= self.toolbar.frame; self.toolbar.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, CGRectGetWidth(rc), CGRectGetHeight(rc)); self.webView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); }]; } else if (lastPostion - currentPostion > kSlide && (currentPostion <= scrollView.contentSize.height-scrollView.bounds.size.height-kSlide)) { lastPostion = currentPostion; //重设frame [UIView animateWithDuration:kAnimationTime animations:^{ CGRect rc = self.navigationController.navigationBar.frame; self.navigationController.navigationBar.frame = CGRectMake(0, 0, CGRectGetWidth(rc), CGRectGetHeight(rc)); rc= self.toolbar.frame; self.toolbar.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - CGRectGetHeight(rc), CGRectGetWidth(rc), CGRectGetHeight(rc)); self.webView.frame = CGRectMake(0, CGRectGetHeight(self.navigationController.navigationBar.frame), [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - CGRectGetHeight(self.navigationController.navigationBar.frame) - CGRectGetHeight(rc)); }]; } }
ps:不要忘记设置
self.webView.scrollView.delegate = self;
demo示例: