skip to Main Content

I have followed all the links on Google and StackOverFlow, unfortunately, I could not find any reliable solution Specifically for iPad devices.

I am using the sample code from Kodeco UISplitViewController. You can download the source code from here and add a UIButton.

I added a Button and added a code for WindowScene RequestGeometryUpdate function.

So, please I need help to Rotate iPad Screen when Pressed on Button. I have attached Error Console Message when pressed on a Button.

Your help will be greatly appreciated.

@IBAction func btnSelector(_ sender: Any) {
    self.rotateToLandsScapeDevice()
}

func rotateToLandsScapeDevice(){
                                
    let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
    if #available(iOS 16.0, *) {
        windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .landscapeLeft), errorHandler: { error in
            print("Error", error)
        })
        self.setNeedsUpdateOfSupportedInterfaceOrientations()
    } else {
        // Fallback on earlier versions
        UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
        UIView.setAnimationsEnabled(true)
    }

}

Error Console Message

Error Domain=UISceneErrorDomain Code=101 "The current windowing mode 
does not allow for programmatic changes to interface orientation." 
UserInfo={NSLocalizedDescription=The current windowing 
mode does not allow for programmatic changes to interface orientation.}

2

Answers


  1. Try to add "Supported interface orientations (iPhone)" and set the item to "Portrait (bottom home button)" in your info.plist file like that

    enter image description here

    Login or Signup to reply.
  2. Your code looks fine. It seems like there is conflict between Upside Down and Landscape here. In Info tab, unchecking the iPad Orientation state Upside Down could fix the problem.

    enter image description here

    Output:

    enter image description here

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