Hiền Đỗ
Hiền Đỗ

Reputation: 13

How to turn on/off Portrait Orientation Lock iPhone in Swift

I've used my code to set the screen's brightness.

UIScreen.main.brightness = CGFloat(0.5)

Can I use the code to enable and disable the screen orientation lock of my iPhone?

Thanks for all the help!

enter image description here

Upvotes: 0

Views: 107

Answers (3)

Visal Rajapakse
Visal Rajapakse

Reputation: 2042

3rd party applications can't manipulate all system settings or other app settings as it is likely going to cause security breaches. It's called sandboxing. Every app has it's own "Sandbox" or to put it simply, a prison where it cannot access/manipulate system settings or other app settings, which make the Apple devices really secure.

Although you can manipulate the orientation of you application using

override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

Upvotes: 0

Abdul Karim Khan
Abdul Karim Khan

Reputation: 4935

You can use this to update device orientation:

UIDevice.current.setValue(UIDeviceOrientation.portrait.rawValue, forKey: "orientation")

Or

UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")

Upvotes: 0

saurabh
saurabh

Reputation: 6775

• Set the shouldAutorotate to False

 override open var shouldAutorotate: Bool {
    return false
 }

• Specify the orientation.

override open var supportedInterfaceOrientations:UIInterfaceOrientationMask {
    return .portrait
}

P.S : You can do this for in a screen in your app, not on the system (iPhone).

Upvotes: 0

Related Questions