如何在纵向模式下锁定viewController [英] How to lock viewController in Portrait mode
问题描述
我在swift中使用iOS应用程序。我使用UITabBarController作为rootViewController。我在一个viewController中有视频列表。此viewController仅支持纵向模式,用户选择视频,然后使用showViewController方法输入playerController,该方法可以同时支持方向(纵向和横向模式)。如果视频完成则播放器控制器弹出到视频列表控制器。一切都很好,但用户可以在视频结束时旋转屏幕(如剩余时间1或0秒),然后视频列表viewController进入横向模式。
我已尝试使用此代码在纵向模式下设置播放器方向。
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.
推荐答案
要在模式下锁定viewController,请执行以下操作:
To lock a viewController in a mode do the following:
在你的AppDeletegate中添加:
In your AppDeletegate add:
var shouldSupportAllOrientation = false
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if (shouldSupportAllOrientation == true){
return UIInterfaceOrientationMask.All
}
return UIInterfaceOrientationMask.Portrait
}
现在转到每个视图并在 viewWillAppear
中添加以下内容:
Now go to each view and add the following in viewWillAppear
:
仅限肖像模式
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appdelegate.shouldSupportAllOrientation = false
所有模式
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版
在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
这篇关于如何在纵向模式下锁定viewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!