Interaction between UINavigationControllers addChildViewController and topViewController

I have something like a modal view controller that I need to display over my other controllers. I do not use the function of a regular modal controller ( presentViewController: and friends), since I need better control over the process. Instead, I use the view controller's restriction function ( addChildViewController: method group).

The containment function makes the code pretty simple. When I need to present a โ€œmodalโ€ view controller, I add it as a child to the view manager hierarchy, and everything works as expected. One small catch is that regular view controllers are wrapped in a navigation controller. Therefore, I have to add a modal controller as a child of the navigation controller, otherwise it will be closed by the navigation panel and toolbar.

Now the problem is that calling addChildViewController: on the navigation controller also sets the new controller as topViewController , as if the controller was clicked using the usual pushViewController: method. This means that while the modal controller is being displayed, the regular controller below it does not receive appearance and rotation callbacks.

Does this sound like a mistake, or am I missing something?

+4
source share
2 answers

I skipped this suggestion in the documentation for addChildViewController:

This method is intended only to enable the user controller to present the container.

So, I think that my error and this scenario are simply not supported. Which sucks, because it is very convenient to create any modal things as ordinary controllers and connect them to a hierarchy, for example, to first-class citizens. I will probably have to rewrite the navigation controller myself in order to have built-in support for this.

+1
source

I had the same problem. I solved this by writing my own custom view controller containing the UINavigationController (added via addChildViewController: , and then exposing the UINavigationController as a readonly property. You can then add your modal view controller as a child of your new custom view controller, and not as a child of the UINavigationController

+2
source

Source: https://habr.com/ru/post/1411472/


All Articles