如何为一个控制器解锁方向?我只允许一个 - 肖像 - 模式用于整个我的应用程序,但只有一个我需要一个横向模式。为此我尝试了下一个片段:
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
override func shouldAutorotate() -> Bool{
return false
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.Portrait
}
但它对我没有帮助。还有其他解决方案吗?
答案 0 :(得分:2)
在Appdelegate中定义一个var:
import UIKit
var isViewAppeared = false
现在在Appdelegate:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if isViewAppeared{
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
}
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
现在在ViewController
你想要的地方:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
isViewAppeared = true
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
isViewAppeared = false
UIView.animateWithDuration(0.4, animations: { () -> Void in
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
})
}
答案 1 :(得分:0)
执行以下操作:
启用常规信息屏幕中的所需方向
每个ViewController.swift
中的
希望有所帮助:)