UINavigationController子视图在隐藏/显示导航栏后不会再回来

时间:2015-02-22 08:58:28

标签: ios objective-c iphone uiview uinavigationbar

我在导航控制器的子视图中苦苦挣扎,导航控制器的位置和尺寸都被限制在导航栏中。当我隐藏导航栏时,子视图消失(预期的行为)。但是当我带回导航栏时,子视图不会跟随。

此子视图只是一个进度视图(我根据某些任务的进度填充颜色的自定义UIView)位于导航栏顶部。

使用我的UINavigationController子类的viewDidLoad中的约束来设置进度视图的位置:

 - (void) viewDidLoad{

// The dimensions here are totally arbitrary since they are going to be defined automatically by the constraints
_progressView = [[CRProgressView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

[self.view addSubview:_progressView];

UINavigationBar *navBar = [self navigationBar];

[self.view addConstraint: [NSLayoutConstraint constraintWithItem:_progressView
                                          attribute:NSLayoutAttributeBottom
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:navBar
                                          attribute:NSLayoutAttributeBottom
                                         multiplier:1
                                           constant:0]];

[self.view addConstraint: [NSLayoutConstraint constraintWithItem:_progressView
                                          attribute:NSLayoutAttributeLeft
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:navBar
                                          attribute:NSLayoutAttributeLeft
                                         multiplier:1
                                           constant:0]];

[self.view addConstraint: [NSLayoutConstraint constraintWithItem:_progressView
                                          attribute:NSLayoutAttributeRight
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:navBar
                                          attribute:NSLayoutAttributeRight
                                         multiplier:1
                                           constant:0]];

[self.view addConstraint: [NSLayoutConstraint constraintWithItem:_progressView
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:navBar
                                                      attribute:NSLayoutAttributeHeight
                                                     multiplier:0
                                                       constant:2]];

 [_progressView setTranslatesAutoresizingMaskIntoConstraints:NO];
 }

从这里一切看起来都很好但是在某些时候,在导航控制器中显示的collectionView中,我调用[self.navigationController setNavigationBarHidden:YES animated:YES]来清除屏幕并在用户向下滚动集合时有额外的空间视图。

如果用户向上滚动,我会向后显示导航栏,但此处显示的导航栏没有进度视图。

以下是用于在collectionView中显示和隐藏导航栏的代码,但我不认为问题来自此处。它正确隐藏并显示navigationBar:

- (void) scrollViewDidScroll:(UIScrollView *)scrollView{

if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y <= scrollView.contentSize.height - self.view.frame.size.height) {
    if (self.lastContentOffsetY > scrollView.contentOffset.y) {
        //Show navigation bar
        if (self.navigationController.navigationBarHidden) {
            [self.navigationController setNavigationBarHidden:NO animated:YES];
        }
    }
    else if (self.lastContentOffsetY < scrollView.contentOffset.y) {

        if (!self.navigationController.navigationBarHidden) {
            [self.navigationController setNavigationBarHidden:YES animated:YES];   
        }
    }

    self.lastContentOffsetY = scrollView.contentOffset.y;
}
}

当我调用[self.navigationController setNavigationBarHidden:NO animated:YES]时,我不明白为什么进度视图没有返回导航栏。

任何帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我不确定在您的情况下它是否100%有效,但是您可以尝试插入此覆盖方法:

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated{
   [super setNavigationBarHidden:hidden animated: animated];
   [self layoutIfNeeded];
}
相关问题