如何仅为一个视图控制器锁定方向



我可以在一个控制器中设置纵向和锁定方向吗?

我确实尝试过:

override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
override func shouldAutorotate() -> Bool{
return false
}

但这对我没有帮助。我还需要补充什么吗?

此代码应该可以工作:

override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
override func shouldAutorotate() -> Bool{
return false
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.Portrait
}

如果它现在对你有效,那么我想你的控制器在另一个控制器中(UINavigationControllerUITabBarControllerUISplitViewController)。在这种情况下,您需要在该父控制器中使用此代码。

如果您的导航控制器包含多个视图控制器,并且您只需要为其中一些禁用方向,那么您需要继承UINavigationController类并在其中编写类似于的内容

class NavigationController: UINavigationController {
var shouldRotate: Bool = true
override func supportedInterfaceOrientations() -> Int {
return shouldRotate ? Int(UIInterfaceOrientationMask.Portrait.rawValue) : Int(UIInterfaceOrientationMask.All.rawValue)
}
override func shouldAutorotate() -> Bool{
return shouldRotate
}
}

然后在您需要禁用方向的控制器中,您可以为您的导航控制器禁用它:

class ViewController: UIViewController {
var lastControllerRotationStatus: Bool?
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if let navigationController = self.navigationController as? NavigationController {
lastControllerRotationStatus = navigationController.shouldRotate
navigationController.shouldRotate = false
}
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
if let navigationController = self.navigationController as? NavigationController {
navigationController.shouldRotate = lastControllerRotationStatus ?? true
}
}
}

但请记住,您需要恢复旧的旋转状态后,您的控制器将推出导航控制器。在这个例子中,我在更改旋转状态之前保存它,并在控制器消失后恢复它。你也可以使用其他方法。例如,您可以重载UINavigationController方法popViewController,然后将shouldRotate设置为false。

与控制器变量设置的方法相同,您可以使用AppDelegate方法application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int控制旋转

那么您就不需要从导航控制器继承。

您的AppDelegate类代码看起来像:

class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var shouldRotate = true
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
return shouldRotate ? Int(UIInterfaceOrientationMask.All.rawValue) : Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
}

你的控制器代码看起来像:

class ViewController: UIViewController {
var lastControllerRotationStatus: Bool?
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
lastControllerRotationStatus = appDelegate.shouldRotate
appDelegate.shouldRotate = false
}
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = lastControllerRotationStatus ?? true
}
}
}

如果你想要纵向,但你只想锁定一个视图控制器(例如在横向),你可以这样做:

这适用于Swift 2.0

AppDelegate

var shouldRotate = true
//MARK: - Func to rotate only one view controller
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if (shouldRotate == true){
return UIInterfaceOrientationMask.All
}
return UIInterfaceOrientationMask.Landscape
}

ViewController

override func viewDidLoad() {
super.viewDidLoad()
// 1. lock the rotation
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appdelegate.shouldRotate = false
// 2. Force the device in landscape mode when the view controller gets loaded
UIDevice.currentDevice().setValue(UIInterfaceOrientation.LandscapeRight.rawValue, forKey: "orientation")
}
override func shouldAutorotate() -> Bool {
// 3. Lock autorotate
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.LandscapeRight, UIInterfaceOrientationMask.LandscapeLeft]
}

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
// 4. Only allow Landscape
return UIInterfaceOrientation.LandscapeRight
}
// 5. Restoring the locked-rotation before controller disappeared
override func viewWillDisappear(animated: Bool) {
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appdelegate.shouldRotate = true
}

**添加到您的AppDelegate文件:*

var orientationLock = UIInterfaceOrientationMask.all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.orientationLock
}
struct AppUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
}
}

添加到要强制定向的视图控制器:

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
AppDelegate.AppUtility.lockOrientation(.portrait)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
AppDelegate.AppUtility.lockOrientation(.all)
}

我在一个类似的问题上发现了这一点。链接

"首先,我真的很感谢泽尔布,他给了我这个答案,让我明白了。为了将一个视图控制器的设备方向设置为纵向,将另一个设置为双向,您可以在AppDelegate.swift上使用支持InterfaceOrientationsForWindow 的功能进行设置

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {
return checkOrientation(self.window?.rootViewController?)// This is the custom function that u need to set your custom view to each orientation which u want to lock
}

这里有一个功能,可以检查每个视图控制器,根据您的需要设置"纵向"或"全方位"。

func checkOrientation(viewController:UIViewController?)-> Int{
if(viewController == nil){
return Int(UIInterfaceOrientationMask.All.rawValue)//All means all orientation
}else if (viewController is SignInViewController){
return Int(UIInterfaceOrientationMask.Portrait.rawValue)//This is sign in view controller that i only want to set this to portrait mode only
}else{
return checkOrientation(viewController!.presentedViewController?)
}
}

并且不要忘记将要在包括方位的属性检查器处锁定的视图控制器设置为"0";肖像;而其他的则是";推断的">

希望能有所帮助。

只需将其放入viewDidPeare:

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

Swift 3答案:

在视图控制器中,您要锁定的方向

override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}

改编自如何使用快速仅锁定主视图的纵向方向

最新更新