Child View Controller to Rotate While Parent View Controller Does Not

前端 未结 3 1710
既然无缘
既然无缘 2020-12-12 21:21

What I am Trying to Do:

Parent View that is managed by Parent View Controller SHOULD NOT ROTATE.

Child View that i

3条回答
  •  轮回少年
    2020-12-12 22:01

    The effect achieved in the Camera app is similar to using a combination of:

    • the technique described in Technical Q&A QA1890 for preventing a view from rotating; and
    • the use of child UIStackViews to rearrange subviews on device rotation (see, e.g., the SimpleExampleViewController class in the AdaptiveElements sample code shown in WWDC 2016 Session 233).

    The technical note explains that the underlying implementation of autorotation involves applying a transform directly to the application's window:

    Autorotation is implemented by applying a rotation transform to the application's window when the system determines that the interface must rotate. The window then adjusts its bounds for the new orientation and propagates this change down through the view controller hierarchy via calls to each view controller's and presentation controller's viewWillTransitionToSize:withTransitionCoordinator: method.

    Note that the Camera app does not opt out of autorotation: when using the Camera app, the orientations of Notification Center and Control Center reflect the device orientation. It is only particular views within the Camera app that appear to opt out of autorotation. In fact, this answer suggests that such apps typically utilise the videoGravity and videoOrientation properties provided by AVFoundation classes such as AVCaptureVideoPreviewLayer and AVCaptureConnection.

    The approach you should take depends on whether you only want to stop a parent view from rotating or whether you want to stop a container view controller such as UINavigationController from rotating (i.e. lock device orientation).

    If the former, use the technique described in the technical note to fix the orientation of the parent view, and (for iOS 9) wrap rotating subviews in a UIStackView and use the axis property to rearrange the subviews on device rotation, or (for iOS 8) manually rotate the subviews on device rotation (as to which see this answer and this sample code).

    If the latter, the approach you're taking is on the right track. For sample code, see this answer. You'll need to share your code and more information about your view controller hierarchy for us to diagnose quirks involving Notification Center.

    Addendum: The reason why calls to shouldAutorotate and supportedInterfaceOrientations were not being propagated to child view controllers was explained in Technical Q&A QA1688:

    Beginning with iOS 6, only the topmost view controller (alongside the UIApplication object) participates in deciding whether to rotate in response to a change of the device's orientation. More often than not, the view controller displaying your content is a child of UINavigationController, UITabBarController, or a custom container view controller. You may find yourself needing to subclass a container view controller in order to modify the values returned by its supportedInterfaceOrientations and shouldAutorotate methods.

    In Swift, you can override those methods by using extensions.

提交回复
热议问题