如何在纵向模式下lockingviewController
我很快就在iOS应用程序上开玩笑了。 我已经使用UITabBarController作为rootViewController。 我有一个viewControllervideo列表。 这个viewController只支持肖像模式和用户select一个video,然后使用showViewController方法进入playerController,它可以同时支持方向(纵向和横向模式)。 如果video播放完成,playerController会popupvideo列表控制器。每个事情都很好,但用户可以在video播放完成后旋转屏幕(如剩余时间1秒或0秒),然后video列表viewController进入横向模式。 我已经试过这个代码在纵向模式下设置玩家的方向。
let value = UIInterfaceOrientation.Portrait.rawValue UIDevice.currentDevice().setValue(value, forKey: "orientation")
但没有工作。 如何解决这个问题。
要在某种模式下lockingviewController,请执行以下操作:
在你的AppDeletegate中添加:
var shouldSupportAllOrientation = false func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { if (shouldSupportAllOrientation == true){ return UIInterfaceOrientationMask.All } return UIInterfaceOrientationMask.Portrait }
现在转到每个视图,并在viewWillAppear
添加以下内容:
只有Portait模式
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate appdelegate.shouldSupportAllOrientation = false
所有模式
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate appdelegate.shouldSupportAllOrientation = true
更新
如果要在更改视图/选项卡时再次从风景再现到肖像,请添加以下代码
let value = UIInterfaceOrientation.Portrait.rawValue UIDevice.currentDevice().setValue(value, forKey: "orientation")
Swift 3.x版本
添加这个在你的AppDelegate类
var shouldSupportAllOrientation = false func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { if (shouldSupportAllOrientation == true){ return UIInterfaceOrientationMask.all } return UIInterfaceOrientationMask.portrait }
把这个添加到你的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
您首先必须进入您的一般项目设置,并确保您的“设备方向”设置为纵向(和可选倒置)
然后在info.pList文件中,
检查钥匙支持的接口方向。 删除除了唯一的肖像键以外的所有键。 查看下面的图片。
这将迫使您的应用程序以纵向方式运行。 即使以横向模式closures播放器,您的应用程序也会回到纵向模式。