Reputation: 3070
I am trying to build a scroll view with a "floating" header using auto-layout. To be more exact I am trying to build a calendar view with several columns. Each of those column should have its own header which should float on top while the column can be scrolled vertically below it.
If everything works it looks like this:
As you can see there are several columns and a page control to indicate how many more pages of columns are available.
However when swiping/panning (or even just trying to swipe) to switch to the next page, the constraints keeping the header labels on top are removed and they disappear to the top of the scroll view (where they are in the storyboard).
Column headers are not visible due to being scrolled offscreen
Column headers are positioned at top of the scroll view (and the height is wrong).
A user can switch between dates ("Today" button at the top with left-right-arrows) and switch between the people displayed. (swipe/pan on view)
Boh interactions are realized with UIPageViewController
s inside one another. The outer page view controller switches between dates, the inner one between the pages of columns (with people).
The time view is contained in the outer page view controller but not the inner one.
So the hierarchy of views and controllers looks like this:
Calendar PageViewController (the one controlled via buttons in the navigation bar)
-- Scroll View of Page View Controller
---- Team View Controller (with view)
------ Header View reference (see screenshot 2)
------ Scroll View for vertical scrolling
-------- Time View (the one on the left)
-------- People PageViewController (the one controlled by swiping left/right)
---------- ScrollView of Page View Controller
------------ ViewController for a Single Page (with view)
--------------(1-n) Container View Controller (several of those, in the example 4 per page)
---------------- Column View Controller
------------------ Header View (A UIVisualEffectsView with a label)
------------------ calendar column view (the one doing the horizontal stripes)
Two pin the header views of the individual column view controllers to the top I use a reference view outside of the inner page view controller and outside of the vertical scroll view. I called it Header View reference
in the overview above and you can see it quite nicely in the broken example:
It's a simple UIVisualEffectsView that I constrained to be at the top left with a fixed height and same width as the time view. So this view has the correct position and height I want all my header views to have and I use that to constraint the individual column header views to it in code (all other constraints are set up in storyboards) in the updateViewConstraints
method of each ColumViewController
like so:
- (void)updateViewConstraints
{
DDLogDebug(@"updating view constraints in colum view controller for %@", self.employee.displayName);
UIView *baseView = self.headerViewReferenceContainer.viewForHeaderContainerViewConstraints;
UIView *containerReference = self.headerViewReferenceContainer.headerContainerViewReference;
NSParameterAssert([containerReference isDescendantOfView:baseView] && [self.headerContainerView isDescendantOfView:baseView]);
[baseView addConstraint:[NSLayoutConstraint constraintWithItem:self.headerContainerView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:containerReference
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0]];
[baseView addConstraint:[NSLayoutConstraint constraintWithItem:self.headerContainerView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:containerReference
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0]];
[super updateViewConstraints];
}
baseView
is the view to which the constraints should be added. It must be a superview of both the header reference view and the header view of the column view controller. At the moment this will be the view of the Team View Controller
above (the one containing the vertical scroll view).
containerReference
is the Header View Reference view as seen in the screenshot above.
So I constrain the column header views to have the same top position and same height as the reference view. (the width and x position depends on the column)
As you can see in the broken screenshots the Header View reference is always positioned correctly. Also, on loading the view controllers and there views, the constraints are setup correctly.
Then when starting a pan gesture which switches the page of the PeoplePageView Controller and loads the next ViewController for a Single Page
the constraints somehow disappear and the header views move to the top of the scroll view due to no longer being pinned to the top.
The next View Controller for a Single Page
has the constraints set up correctly, but only until it snaps into place.
So while panning the constraints of the view controller to display are setup correctly. But as soon as it snaps into place (feels like it happens at -viewDidAppear
) the constraints are removed as well.
I don't understand why and how the constraints are removed.
It could be due to one of the related views disappearing, but
baseView
doesn't change at all on swiping left right as it is a superview of the paging scroll viewI used to have an issue with the layout only falling into place AFTER the paging finished. This was due to my constraints relating to the top layout constraints as described in iOS 8 UIPageViewController Applying Constraints After Transitions.
As a result I use a fixed distance from the super view's top to pin my Header View Reference
instead of pinning it to the top layout constraint.
So the bug of the UIPageViewController not having a correct top layout constraint on paging shouldn't be the issue either.
I do not add any views programatically. All my Storyboards use auto layout and view controller are added to each other via Container View Controllers in the Storyboard or implementing a UIPageViewControllerDatasource
and instantiating the view controller from a xib. So there shouldn't be any issues related to translatesAutoResizingMasks
. Also, it works on load, it only breaks on paging, this doesn't fit the usual translatesAutoResizingMasks = YES
problem.
I also do not get any errors about conflicting constraints or the view hierarchy not being prepared for constraints, the working constraints are simply removed.
I due have one issue which I think is unrelated, but I'll list it here for completeness' sake: I get an error due to the layout constraints of the labels inside the column header views being added to early:
The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7af4df20 UILabel:0x7af4e0d0'Susanne'.width == UIVisualEffectView:0x7af4de70.width>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
However, that message complains about the relation between the label and its containing view (the header view of the column), not about the "outer" constraints of the header view. Also, it warns that the app will crash if the constraints need to be resolved too early. The app does not crash, so the timing seems to be right. Also, the constraints the app complains about are setup in a storyboard, so I don't see how I could get the timing wrong.
tl;dr: Constraints that reach through several levels of the view hierarchy from inside a UIPageViewController to a view outside of it are removed (without warning, comment, reason or anything). I don't understand why and how to prevent that from happening or at what point to add them again.
UPDATE:
I added the following call to viewWillAppear:
:
[self.view setNeedsUpdateConstraints]
This has the following effect:
viewWillAppear:
on the original view controller if it "snaps back" (to offset the previously issued viewWillDisappear
call).viewWillAppear
is called the constraints are fixed for a short time until they are removed again (usually when the scroll ends).If I add the same call to viewDidAppear
instead of viewWillAppear
the following happens:
Now if I add [self.view setNeedsUpdateConstraints]
to both methods the following happens:
Upvotes: 3
Views: 2336
Reputation: 3070
Ok, I still think it is a bug, but I found a workaround:
I basically have two constraint I want to keep and which sometimes disappear. I create properties for these, but make them weak:
@property (weak) NSLayoutConstraint *headerViewHeightConstraint;
@property (weak) NSLayoutConstraint *headerViewTopConstraint;
I then assign the created constraints to these properties in updateViewConstraints
like this:
NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.headerContainerView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:containerReference
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0];
[baseView addConstraint:topConstraint];
self.headerViewTopConstraint = topConstraint;
By using a weak
reference I can check whether the constraint exists without keeping it around if nothing else references it. This way, when the constraint is removed from the view (for whatever reason) it will be deallocated an my property will be nil. I can use that to check whether I need to reestablish the constraint like this:
- (void)reestablishHeaderViewConstraintsIfNecessary
{
if (!self.headerViewTopConstraint || !self.headerViewHeightConstraint) // if any of the constraints is missing
{
[self.view setNeedsUpdateConstraints];
}
}
Now I just need a place to call that method from, to make sure it is called whenever the constraints might have been removed. As described in the question viewWillAppear
and viewDidAppear
are not good enough. Instead I use viewWillLayoutSubviews
. This is called whenever the bounds change (e.g. many times during scrolling), which is quite often, but because I wrap the actual constraint invalidation this should be cheap enough if nothing needs to be done:
- (void)viewWillLayoutSubviews
{
[self reestablishHeaderViewConstraintsIfNecessary];
[super viewWillLayoutSubviews];
}
I think it is important to use viewWillLayoutSubviews
, not viewDidLayoutSubviews
ad invalidating the layout in viewDidLayoutSubviews
leads to an exception but I'm not sure. This is also the reason why I put the call in front of the call to [super viewWillLayoutSubviews]
, but I didn't try whether it would work with the call appearing after it.
Upvotes: 0
Reputation: 1055
Why don't you set the top of the headerView to the top of the baseview. i.e. the view that contains the scrollview. This will fix your issue for sure.
Upvotes: 0
Reputation: 1055
Try setting your constraints in viewDidLayoutSubviews
. I am not quite certain but I remember solving a constraint related issue this way and it seemed to work.
Upvotes: 0
Reputation: 8014
Is there a chance that updateViewConstraints
is being called more than once causing multiple additions of the same constraints which is then causing an issue. I believe this can happen quite easily.
I notice in numerous discussions that many seem to recommend not doing the create and add in this function, (viewDidLoad seems preferred) and to only do the constraint value settings in this function to minimize the issue of multiple calls but to ensure the sizes are correct. Another option seems to be to put a Boolean guard for the controller around constraint addition to make sure it only occurs once in the lifetime of the controller.
Might not be your issue, but thought worth mentioning.
Upvotes: 3