I'm struggling with a subview of my navigation controller whose position and dimensions are constricted to the navigation bar. When I hide the navigation bar, the subview disappears (expected behavior). But when I'm bringing the navigation bar back the subview does not follow.
This subview is just a progress view (a custom UIView that I fill with a color depending on the progress of some tasks) on top of my navigation bar.
The positionning of the progress view is set with constraints in the viewDidLoad of my UINavigationController subclass :
- (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];
}
From here everything looks fine but at some point, in the collectionView displayed in the navigation controller I call [self.navigationController setNavigationBarHidden:YES animated:YES] to clear the screen and have extra space when the user scrolls down in the collection view.
If the user scrolls up I show the navigation bar back but here the navigation bar appears without the progress view.
Here is the code used for showing and hiding the navigation bar in the collectionView, but I don't think the issue comes from here. It correctly hides and shows the 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;
}
}
I don't understand why the progress view does not come back with the navigation bar when I'm calling [self.navigationController setNavigationBarHidden:NO animated:YES].
Any help will be greatly appreciated. Thanks in advance.
self.view.setNeedsUpdateConstraints()
at the end of your scrolling method? – Romain