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
374 views
in Technique[技术] by (71.8m points)

ios - How to lock viewController in Portrait mode

I am woking on iOS application in swift. I have used UITabBarController as a rootViewController. I have list of videos in one viewController. This viewController supports only portrait mode and user select a video then enter playerController using showViewController method, that can support both orientation (Portrait and Landscape mode). If video finished then playerController pop to video list controller.Every things is fine but user can rotate screen during the finishing the video(like remaining time 1 or 0 second) then video list viewController enter in Landscape mode. I have tried this code for set player orientation in portrait mode.

let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")

But not worked. How to fixed this issue.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

To lock a viewController in a mode do the following:

In your AppDeletegate add:

var shouldSupportAllOrientation = false
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
    if (shouldSupportAllOrientation == true){
        return UIInterfaceOrientationMask.All
    }
    return UIInterfaceOrientationMask.Portrait
}

Now go to each view and add the following in viewWillAppear:

Portait mode only

let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appdelegate.shouldSupportAllOrientation = false

All modes

let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appdelegate.shouldSupportAllOrientation = true

Update
If you want to go from landscape back to portrait again when you change view/tab, add the following code

let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")

Swift 3.x version

Add this in your AppDelegate class

var shouldSupportAllOrientation = false
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if (shouldSupportAllOrientation == true){
        return UIInterfaceOrientationMask.all
    }
    return UIInterfaceOrientationMask.portrait
}

Add this in your viewController

// Portrait mode
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.shouldSupportAllOrientation = false

// All modes
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.shouldSupportAllOrientation = true

Swift 4 version:

In your AppDelegate, add the following:

var shouldSupportOrientation: UIInterfaceOrientationMask = .portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldSupportOrientation
}

In each viewController add the following:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let appdelegate = UIApplication.shared.delegate as! AppDelegate
    appdelegate.shouldSupportOrientation = .portrait // set desired orientation

    let value = UIInterfaceOrientation.portrait.rawValue // set desired orientation
    UIDevice.current.setValue(value, forKey: "orientation")
}

This will lock your view and rotate it.


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

2.1m questions

2.1m answers

60 comments

57.0k users

...