我有一些我不明白的问题。
我想在我的滚动上隐藏我的导航栏,我做了两个不同的解决方案:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
lastOffsetY = scrollView.contentOffset.y;
}
- (void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
bool hide = (scrollView.contentOffset.y > lastOffsetY);
[[self navigationController] setNavigationBarHidden:hide animated:YES];
NSLog(@"%f %f %f %f", self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.layer.frame.origin.y, self.webView.frame.origin.y, self.view.superview.frame.origin.y);
}
或第二个解决方案:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect frame = self.navigationController.navigationBar.frame;
CGFloat size = frame.size.height - 21;
CGFloat framePercentageHidden = ((20 - frame.origin.y) / (frame.size.height - 1));
CGFloat scrollOffset = scrollView.contentOffset.y;
CGFloat scrollDiff = scrollOffset - lastOffsetY;
CGFloat scrollHeight = scrollView.frame.size.height;
CGFloat scrollContentSizeHeight = scrollView.contentSize.height + scrollView.contentInset.bottom;
if (scrollOffset <= -scrollView.contentInset.top) {
frame.origin.y = 20;
} else if ((scrollOffset + scrollHeight) >= scrollContentSizeHeight) {
frame.origin.y = -size;
} else {
frame.origin.y = MIN(20, MAX(-size, frame.origin.y - scrollDiff));
}
[self.navigationController.navigationBar setFrame:frame];
[self updateBarButtonItems:(1 - framePercentageHidden)];
lastOffsetY = scrollOffset;
[self.view setNeedsUpdateConstraints];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self stoppedScrolling];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
willDecelerate:(BOOL)decelerate
{
if (!decelerate) {
[self stoppedScrolling];
}
}
- (void)stoppedScrolling
{
CGRect frame = self.navigationController.navigationBar.frame;
if (frame.origin.y < 20) {
[self animateNavBarTo:-(frame.size.height - 21)];
}
NSLog(@"%f %f %f %f", self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.layer.frame.origin.y, self.webView.frame.origin.y, self.view.superview.frame.origin.y);
}
- (void)updateBarButtonItems:(CGFloat)alpha
{
[self.navigationItem.leftBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem* item, NSUInteger i, BOOL *stop) {
item.customView.alpha = alpha;
}];
[self.navigationItem.rightBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem* item, NSUInteger i, BOOL *stop) {
item.customView.alpha = alpha;
}];
self.navigationItem.titleView.alpha = alpha;
self.navigationController.navigationBar.tintColor = [self.navigationController.navigationBar.tintColor colorWithAlphaComponent:alpha];
}
- (void)animateNavBarTo:(CGFloat)y
{
[UIView animateWithDuration:0.2 animations:^{
CGRect frame = self.navigationController.navigationBar.frame;
CGFloat alpha = (frame.origin.y >= y ? 0 : 1);
frame.origin.y = y;
[self.navigationController.navigationBar setFrame:frame];
[self updateBarButtonItems:alpha];
}];
}
第一个,导航栏动画很棒,webView占据了我的屏幕界限,状态栏位于webView下,这不是很好。
第二个,导航栏正确运行动画,我的状态栏背景正确,但我的webView保持静止,我在navigationBar旧位置下面有一个白色背景,而webView没有#t按照我的导航栏。
我的问题是,我怎样才能像setNavigationBarHidden
苹果功能一样?
感谢&#39; S
答案 0 :(得分:0)
我大致得到你想要的动画类型。
对于第二个解决方案,您还需要更改UIWebView的frame
属性。
// get UIWebView frame here
CGRect webViewRect = _webView.frame;
if (scrollOffset <= -scrollView.contentInset.top) {
frame.origin.y = 20;
} else if ((scrollOffset + scrollHeight) >= scrollContentSizeHeight) {
frame.origin.y = -size;
} else {
frame.origin.y = MIN(20, MAX(-size, frame.origin.y - scrollDiff));
}
webViewRect.origin.y = frame.origin.y + frame.size.height;
if ( webViewRect.origin.y != _webView.frame.origin.y ) {
[_webView setFrame:webViewRect];
}
我没有关于您的视图架构的完整图片,但如果向上移动_webView
,_webView
最终在底部过短,则可以将_webView
的高度增加到相当于导航栏的高度,并将其内容设置为底边,导航栏的高度。像这样:
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height+self.navigationController.navigationBar.frame.size.height)];
[_webView.scrollView setContentInset:UIEdgeInsetsMake(0.0, 0.0, self.navigationController.navigationBar.frame.size.height, 0.0)];