iosswiftuiviewcontrollerios13uimodalpresentationstyle

UIViewController automatic presentation on top of full screen presentation


I have a few UIViewControllers that present on top of one another using modalPresentationStyle = .fullScreen. I want to present another one on top using the .automatic style. This works okay, but does not have the card-like style as expected (where the view controller underneath shrinks away). Is there any way I can achieve this style?

I tried making the current view controller the root view controller as such:

UIApplication.shared.keyWindow?.rootViewController = self.navigationController

This works partially, but I end up with a strange white void instead of the expected black abyss behind the cards. I tried explicitly setting the background color of the window to black:

UIApplication.shared.keyWindow?.backgroundColor = .black

However, this did not change anything.


Solution

  • Thanks to @de. for the idea of using the UI debugger. I was able to fix this styling issue with the following code:

    let window = UIApplication.shared.keyWindow
    window?.rootViewController = self.navigationController
    window?.subviews.last?.backgroundColor = .black
    

    When modifying the root view controller, it seems the system creates a copy of the view controller and throws a UITransitionView behind it. This transition view is a clear color, allowing color from the original view controller to come through. I had to set the color on the transition view to black (the last subview of the window).