Rotation only in one ViewController

后端 未结 13 1881
我在风中等你
我在风中等你 2020-12-05 03:20

I am trying to rotate one view while all other views (5) are fixed to portrait. The reason is that in that one view I want the user to watch pictures which he saved before.

13条回答
  •  暖寄归人
    2020-12-05 03:57

    Just wanted to share my solution as someone who has spent too much time rotating one view controller in the app:

    var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { get }
    

    overriding this UIViewController method helped me do what I need.

    1. On the view controller that you want to rotate do this for landscape left rotation:

    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { return UIInterfaceOrientation.landscapeLeft }

    1. Make sure you enable rotation in the desired directions from the project settings:

    1. And add this to AppDelegate to disable other screens' rotation:

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { return .portrait }

提交回复
热议问题