Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

swift - iOS. How to enable and disable rotation on each UIViewController?

I have an UIViewController, I want to disable or enable rotation of the screen in different scenarios

Example:

if flag {
   rotateDevice = false
}
else {
   rotateDevice = true
}

How can I do that?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I have the answer. On AppDelegate, if you rotate device, push viewcontroller, etc. This function always call

Update for swift 3/4

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
{
    return self.restrictRotation
}

self.restrictRotation is a custom parameter.

How to use:

In Appdelegate:

 var restrictRotation:UIInterfaceOrientationMask = .portrait

In ViewController:

When method ViewDidLoad or viewWillAppear is called. We will change like this:

(UIApplication.shared.delegate as! AppDelegate).restrictRotation = .all 

and then this method on AppDelegate will be called.

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...