Search code examples
iosswiftswift3orientationios11

Change orientation programmatically with button - iOS


I have a view controller and I would like to have the following experience. Inside the view controller i got a button which force rotate the orientation to landscape right.

In Deployment Info - Device Orientation i have enabled "Landscape Right" and "Portrait".

I want to mention that in my device i have enabled the "Portrait Orientation Lock" that's why i want a button to rotate the orientation programmatically with the following code.

let rotateButton: UIButton = {
    let btn = UIButton(type: .system)
    btn.setTitle("Rotate", for: .normal)
    btn.setTitleColor(.red, for: .normal)
    btn.addTarget(self, action: #selector(rotateTapped), for: .touchUpInside)
    return btn
}()

@objc func rotateTapped() {
    let value = UIInterfaceOrientation.landscapeRight.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
}

So the code above works properly and the screen is rotating. Although i want when the user rotates back to Portrait, the screen to rotate to portrait again without any button pressed. When the "Portrait Orientation Locked" is on, the screen is not rotating back to portrait.

I've tried the following codes with no luck.

1)

    NotificationCenter.default.addObserver(self, selector: #selector(rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)


@objc func rotated() {
    if UIDevice.current.orientation.isLandscape {
        print("Landscape") //when the user taps the button this is being printed.
    } else {
        print("Portrait") //when the user rotates back to portrait this is NOT being printed.
    }
}

and 2)

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.current.orientation == .landscapeRight {
        let value = UIInterfaceOrientation.portrait.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
    }
}

Any idea of what can it be to change back to portrait when user rotates the phone again?


Solution

  • I don't have a swift code. Below are the objective c code, It worked for me. You can convert it into swift as per your requirement.

    Ojective C

    UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];    
    [UIViewController attemptRotationToDeviceOrientation];
    

    Update

    Swift 4.0

    var value  = UIInterfaceOrientation.landscapeRight.rawValue
    if UIApplication.shared.statusBarOrientation == .landscapeLeft || UIApplication.shared.statusBarOrientation == .landscapeRight{
       value = UIInterfaceOrientation.portrait.rawValue
    }
    
    UIDevice.current.setValue(value, forKey: "orientation")
    UIViewController.attemptRotationToDeviceOrientation()
    

    The above code has already been tested by me and it is working fine. In case the above code is not worked for you, Then execute it after some delay using performSelector.

    Update 09 Feb 2023(iOS 16)

    class OrientationManager {
        static var landscapeSupported: Bool = false
    }
    

    AppDelegate.swift

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

    When you want to change the orientation on button click

    @IBAction func btnChangeClick(){
            DispatchQueue.main.async {
                OrientationManager.landscapeSupported = !OrientationManager.landscapeSupported
                let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
                windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: OrientationManager.landscapeSupported ? .landscape : .portrait))
                self.setNeedsUpdateOfSupportedInterfaceOrientations()
            }
        }