-1

I change view controller from one to other. The phone was in Landscape orientation when first controller was active. Second controller supports only portrait orientation. When it becomes active it for a moment has landscape orientation, so it's content is drawn for landscape orientation also. Then view controller rotates to portrait orientation, but the redrawing of the content doesn't happens.

In viewWillTransition I call view.setNeedsDisplay() but that doesn't help too.

To solve the problem I made initUI function which is called in viewDidLoad only if main window is in portrait orientation. In other case I call initUI in viewWillTransition.

I want to know what code should I write in such standard situation.

Hihikomori
  • 950
  • 6
  • 12

1 Answers1

0

If you want to restrict the SecondViewController to only portrait. You can use a method of AppDelegate. In that method you can check the current TopViewController, and based on that you can return the orientation.

Please check the code below.

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if (window?.rootViewController as? UINavigationController)!.children.last!.isKind(of: SecondViewController.self){
        return .portrait
    }else{
        return .all
    }
}

To get the TopViewController you can use this extension: https://stackoverflow.com/a/50656239/5973760

Samir Bagaria
  • 130
  • 1
  • 10
  • Second view controller already restricted to portrait orientation. But it loads for a short time in landscape orientation, then rotates. – Hihikomori Jun 03 '23 at 01:38