首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >隐藏navigationBar

隐藏navigationBar
EN

Stack Overflow用户
提问于 2014-11-10 11:24:27
回答 1查看 577关注 0票数 0

我有一些不明白的问题。

我想把我的导航栏藏在我的滚动条上,我做了两个不同的解决方案:

代码语言:javascript
复制
- (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);
}

或者第二种解决方案:

代码语言:javascript
复制
- (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不跟随我的导航栏。

我的问题是,我怎样才能使像setNavigationBarHidden苹果功能?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-10 14:45:28

我大概知道你想要什么样的动画。

对于第二个解决方案,还需要更改UIWebView的UIWebView属性。

代码语言:javascript
复制
// 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的高度,相当于导航条的高度,并将其内容嵌入到导航栏的底部边缘,即导航栏的高度。就像这样:

代码语言:javascript
复制
_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)];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26842699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档