我的导航控制器的一个子视图的位置和尺寸被限制在导航栏中。当我隐藏导航栏时,子视图将消失(预期行为)。但是,当我将导航栏带回时,子视图不会跟随。
这个子视图只是导航栏上的一个进度视图(一个自定义的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 animation:YES]来清除屏幕,并在用户在集合视图中向下滚动时获得额外空间。
如果用户向上滚动,我会显示导航栏,但此处显示的导航栏没有进度视图。
以下是用于在collectionView中显示和隐藏导航栏的代码,但我不认为问题来自于此。它正确地隐藏和显示导航栏:
- (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]时,进度视图没有与导航栏一起返回。
任何帮助都将不胜感激。提前谢谢。
我不确定它在您的情况下是否100%有效,但您可以尝试插入以下覆盖方法:
- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated{
[super setNavigationBarHidden:hidden animated: animated];
[self layoutIfNeeded];
}