How to set the specified page rotation without having it rotate automatically? I found that the screen can rotate only when var shouldAutorotate
returns yes, but I don't want to do that automatically,I want to control the moment of rotate by myself
Asked
Active
Viewed 118 times
0

Vadim Kotov
- 8,084
- 8
- 48
- 62

kivan cheng
- 1
- 1
-
I set VC's static variable ‘canRotate’, set it to true when it needs to be rotated, and set it to fals after the rotation is complete. `override var shouldAutorotate: Bool { return canRotate }` – kivan cheng May 14 '19 at 01:58
-
the `canRotate` is seted false after received the UIDevice.orientationDidChangeNotification, and I found that the screen can't rotate sometime in iphoneX, iOS 12.1 when `orientationDidChangeNotification` comes (Although this is a small probability event) – kivan cheng May 14 '19 at 01:59
-
the rotate screen code: ```UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")``` – kivan cheng May 14 '19 at 01:59
-
I resolved it use this before rotate code: ```if !UIDevice.current.isGeneratingDeviceOrientationNotifications { UIDevice.current.beginGeneratingDeviceOrientationNotifications() }``` – kivan cheng May 20 '19 at 12:39
1 Answers
0
Swift 4+:
Use following code to rotate screen when required without any auto rotation as you want.
func rotateToLandscapeRight() {
UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
}
func rotateToLandscapeLeft() {
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
}
func rotateToPortratit() {
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
}

Rajesh Loganathan
- 11,129
- 4
- 78
- 90