How to lock viewController in Portrait mode

2020-02-06 12:05发布

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.

2条回答
时光不老,我们不散
2楼-- · 2020-02-06 12:11

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.

查看更多
够拽才男人
3楼-- · 2020-02-06 12:14

You first must go to your general project settings and make sure your "Device Orientation" is set to Portrait (and optionally Upside Down)

enter image description here

Then In the info.pList file,

Check the key Supported interface orientations. Delete all its keys except the only portrait key. Review the below image for it.

enter image description here

This will force your app to run in portrait. Even when you close your player in landscape mode, your app will move back to portrait mode.

查看更多
登录 后发表回答