Swift - How to detect orientation changes

后端 未结 13 1793
余生分开走
余生分开走 2020-11-29 19:49

I want to add two images to single image view (i.e for landscape one image and for portrait another image)but i don\'t know how to detect orientation changes using swift lan

13条回答
  •  抹茶落季
    2020-11-29 20:32

    I believe the correct answer is actually a combination of both approaches: viewWIllTransition(toSize:) and NotificationCenter's UIDeviceOrientationDidChange.

    viewWillTransition(toSize:) notifies you before the transition.

    NotificationCenter UIDeviceOrientationDidChange notifies you after.

    You have to be very careful. For example, in UISplitViewController when the device rotates into certain orientations, the DetailViewController gets popped off the UISplitViewController's viewcontrollers array, and pushed onto the master's UINavigationController. If you go searching for the detail view controller before the rotation has finished, it may not exist and crash.

提交回复
热议问题