Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force update of child view controller's safeAreaInsets

I have a rootViewController that has a child view controller (fullScreenViewController) whose view, from time to time, is displayed above the view (but as a subview) of the rootViewController, taking up the full screen.

i.e. rootViewController.addChildViewController( fullScreenViewController ) rootViewController.view.addSubview( fullScreenViewController.view )

This "full screen presentation" is done with constraints updates, not by using present() (There's good reason for this).

When I first initialize fullScreenViewController and add it as a child view controller of rootViewController, then update the constraints so that it is on screen, the safeAreaInsets are all set properly.

However, when, later on, my rootViewController actually does present() a modal VC, and that VC is dismiss()ed, the safeAreaInsets on the fullScreenViewController are no longer correct, and the content in the fullScreenViewController slides up on top of the status bar.

How can I force the safeAreaInsets on fullScreenViewController to be recalculated properly, so that they match the rootViewController the way they do after setup?

According to the documentation, the rootViewController should be setting the safeAreaInsets properly on all of its childViewControllers, but this doesn't seem to be the case.

Safe Area Insets Correct

Safe Area Insets Incorrect

like image 714
Jeff Avatar asked Jul 06 '18 16:07

Jeff


1 Answers

I had the exact same issue with a child view controller's safeAreaInsets not being properly refreshed after a presentation changed the safe area.

I found that the issue was resolved by making the child's view a direct subview of the parent's view, when before it was a subview of a subview. I guess UIKit code is happier when child VC's views are direct subviews of parent's.

If you can refactor your view hierarchy to this configuration (if not already the case) this might do it.

like image 130
amadour Avatar answered Oct 16 '22 07:10

amadour