重写shouldAutorotate不工作在斯威夫特3
我试图防止一个UIViewController
旋转,我不能实现这一点。重写shouldAutorotate不工作在斯威夫特3
我在做这样的事情:
open override var shouldAutorotate: Bool {
get {
return false
}
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
get {
return .portrait
}
}
而且UIViewControler
剧照旋转。 UIViewController在模态打开的UINavigationController中。
我看了很多这里的问题,没有答案适合我。
在Swift 2中,我用来覆盖shouldAutorotate
,但在Swift 3中,函数不再存在。
我该如何在Swift 3中做我以前在Swift 2中做的事情?
我不知道为什么要投票结束这个问题,如果我可以重现这种行为很多次。 UIViewController
位于模块打开的UINavigationController
内。
这是我所做的解决问题。
我创建这个类,我设置为UINavigationController
是containt,我想阻止UIViewController
旋转
class NavigationController: UINavigationController {
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
}
而且多数民众赞成它,它为我工作
但是如果您只想要特定的UIViewController不旋转而不是整个导航堆栈?这个答案看起来像一个更好的:http://stackoverflow.com/a/39805665 –
@RoyShilkrot这是可以的,问题的目标是如何防止一个视图控制器内的一个导航控制器的旋转,而不是如何防止整个导航堆栈中的特定视图控制器 – pableiros
添加该代码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)
//let value = UIInterfaceOrientation.landscapeLeft.rawValue
//UIDevice.current.setValue(value, forKey: "orientation")
AppDelegate.AppUtility.lockOrientation(.landscapeLeft)
}
这适用于我,但接受的答案更清晰并且也适用。有没有一个具体的情况下,接受的答案是不可能的,只有这个作品? – paul
你的'supportedInterfaceOrientations'是什么样的? – matt
@matt我编辑的问题添加, – pableiros
谢谢。好的,如果这是一个呈现的视图控制器,它应该只出现在肖像中并留在那里。如果没有发生,那么故事中就必须有更多的东西。你能描述一下这个视图控制器是怎么呈现的吗?向我证明这是一个全屏顶级的视图控制器。 – matt