首页 文章

UINavigationController子视图's won' t隐藏/显示导航栏后返回

提问于
浏览
1

我正在努力使用我的导航控制器的子视图,其位置和尺寸都受到导航栏的限制 . 当我隐藏导航栏时,子视图消失(预期的行为) . 但是,当我带回导航栏时,子视图不会跟随 .

此子视图只是一个进度视图(我根据某些任务的进度填充颜色的自定义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

    我不确定它是否100%适用于您的情况,但您可以尝试插入此覆盖方法:

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

相关问题