Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

ios - How to change Device Orientation programmatically in Swift?

In my app I want to change the device orientation.

I`ve tried it like in the other question with the same topic:

let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")

But this seems only to work if the orientation is already allowed so that the user can change it with rotating the device, and it also change the orientation with an animation. But I want my app to only have one orientation in VC1 and to have only one orientation in VC2. The user should not have influence on the Orientation, and there should no animation. Is this possible?

Sorry for my bad english....

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For each VC declare this variable w/ desired orientation. This is for portrait.

override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .portrait }   

Then on appearance enforce the desired orientation.

override func viewDidAppear(_ animated: Bool) {
  super.viewDidAppear(animated)
  UIView.setAnimationsEnabled(false)
  UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
  UIView.setAnimationsEnabled(true)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...