skip to Main Content

I have been using the attemptRotationToDeviceOrientation function to change the orientation of some ViewController when the user presses a button. However, as of iOS 16, this function has been officially deprecated.

What can I use as the replacement?

2

Answers


  1. According to release note: https://developer.apple.com/documentation/ios-ipados-release-notes/ios-16-release-notes?changes=lat__8_1
    I think you may have to use setNeedsUpdateOfSupportedInterface.

    Deprecations
    [UIViewController shouldAutorotate] has been deprecated is no longer supported. [UIViewController attemptRotationToDeviceOrientation] has been deprecated and replaced with [UIViewController setNeedsUpdateOfSupportedInterfaceOrientations].
    Workaround: Apps relying on shouldAutorotate should reflect their preferences using the view controllers supportedInterfaceOrientations. If the supported orientations change, use `-[UIViewController setNeedsUpdateOfSupportedInterface

    I think you may have to use setNeedsUpdateOfSupportedInterface.

    Login or Signup to reply.
  2. this can be done in two ways.

    personally I prefer this way

    1.keep this function in AppDelegate to handle the orientation(this is must)

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

    2. in which ViewController you want the force orientation, go to that view controller and add these lines in the variable declaring section

    var forceLandscape: Bool = false
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
               forceLandscape ? .landscape : .portrait
    }
    

    we will be updating the forceLandscape so it will get updated, then the supportedInterfaceOrientations also will get updated

    3. Here we are setting the trigger for updating the forceLandscape (we can add these lines of code inside button action for handling IOS 16 force roatation)

    if #available(iOS 16.0, *) {
                self.forceLandscape = true
                guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene else { return }
                self.setNeedsUpdateOfSupportedInterfaceOrientations()
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: {
                    windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: .landscapeRight)){
                            error in
                            print(error)
                            print(windowScene.effectiveGeometry)
                    }
                })
    

    this will update the forceLandscape, so it will check the orientation and update according to it

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search