Home > Enterprise >  How to correctly set a UIViewControllers frame when presenting it on top of another view controller?
How to correctly set a UIViewControllers frame when presenting it on top of another view controller?

Time:11-30

I'm presenting a UIViewController on top of another view controller using the following code:

let realityKitViewController = RealityKitViewController(localModelPath: modelPath)
realityKitViewController.modalPresentationStyle = .overCurrentContext
            
// Hide the qr code scanner
GAPP.tabbarCtrl.hideQrCodeScanner(withAnimation: true, completion: {
    topController.present(realityKitViewController, animated: true, completion: nil)
})

In viewDidLoad() of the presented view controller I then set the frame:

let tabbarHeight = GAPP.tabbarCtrl.getTabbarHeight() - self.view.safeAreaInsets.bottom
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height - tabbarHeight)

However, when I look at the view hierarchy I can see that the UITransitionView and the UIView of the presented view controller both still cover the whole screen instead if having the frame I set in it's viewDidLoad().

view hierarchy

How can I make the UITransitionView and the presented view controllers UIView the same frame I set in the viewDidLoad() of the presented view controller?

CodePudding user response:

Try setting the frame in the viewWillLayoutSubviews() function instead of viewDidLoad(). It seems like your ViewController hasn't all the correct values in the viewDidLoad() function yet. Setting the frame in viewWillLayoutSubviews() will update your frame before every subview layout updates.

  • Related