I have a UIPageViewController
with translucent status bar and navigation bar. Its topLayoutGuide
is 64 pixels, as expected.
However, the child view controllers of the UIPageViewController
report a topLayoutGuide
of 0 pixels, even if they're shown under the status bar and navigation bar.
Is this the expected behavior? If so, what's the best way to position a view of a child view controller under the real topLayoutGuide
?
(short of using parentViewController.topLayoutGuide
, which I'd consider a hack)
This is the approach for the known guide length. Create constrains not to guides, but to view's top with fixed constants assuming guide distance will be.
While this answer might be correct, I still found myself having to travel the containment tree up to find the right parent view controller and get what you describe as the "real
topLayoutGuide
". This way I can manually implementautomaticallyAdjustsScrollViewInsets
.This is how I'm doing it:
In my table view controller (a subclass of
UIViewController
actually), I have this:Notice the category methods in
UIViewController
, this is how I implemented them:Hope this helps :)
I might be wrong, but in my opinion the behaviour is correct. The topLayout value can be used by the container view controller to layout its view's subviews.
The reference says:
To use a top layout guide without using constraints, obtain the guide’s position relative to the top bound of the containing view.
In the parent, relative to the containing view, the value will be 64.
In the child, relative to the containing view (the parent), the value will be 0.
In the container View Controller you could use the property this way:
The Child view controller does not need to know that there are a Navigation and a Status bar : its parent will have already laid out its subviews taking that into account.
If I create a new page based project, embed it in a navigation controller, and add this code to the parent view controllers it seems to be working fine:
Not sure if anyone still got problem with this, as I still did a few minutes ago.
My problem is like this (source gif from https://knuspermagier.de/2014-fixing-uipageviewcontrollers-top-layout-guide-problems.html).
For short, my pageViewController has 3 child viewcontrollers. First viewcontroller is fine, but when I slide to the next one, the whole view is incorrectly offset to the top (~20 pixel, I guess), but will return to normal after my finger is off the screen.
I stayed up all night looking for solution for this but still no luck finding one. Then suddenly I came up with this crazy idea:
My listViewControllers has 3 child viewcontrollers. The one at index 0 has problem, so I firstly set it as root of pageviewcontroller, and right after that set it back to the first view controller (as I expected). Voila, it worked!
Hope it helps!
you can add a constraint in the storyboard and change it in viewWillLayoutSubviews
something like this: