我正在开发一款适用于iPhone的快速应用程序.在我的应用程序中有一个模态视图,我只想在纵向视图中.
我的问题是,我如何以编程方式强制手机不允许旋转?换句话说,我正在寻找不允许以横向模式显示模态视图的代码(打开纵向旋转锁定).
这仅适用于1个模态视图,因此我无法关闭整个应用程序的旋转,否则我只会完全禁用旋转.
我在这里的研究中找到了代码 但是在目标C中,如果有帮助的话.谢谢!
我使用swift为iPhone创建了一个应用程序,它由嵌入在导航控制器中的许多视图组成.我想将主视图锁定为纵向方向,并且仅锁定横向方向的导航控制器的子视图.这是我的意思的一个例子:
我怎样才能做到这一点?
我尝试了下一个:
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)
上
viewWillAppear
Run Code Online (Sandbox Code Playgroud)
但它不起作用.如何在viewWillAppear上旋转屏幕?
UPDATE
我使用下一个代码来锁定方向:
var shouldRotate = true
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if shouldRotate == true {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
} else {
return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue)
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 4.0
private func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if shouldRotate == true {
return Int(UIInterfaceOrientationMask.portrait.rawValue)
} else {
return Int(UIInterfaceOrientationMask.landscapeLeft.rawValue)
}
}
Run Code Online (Sandbox Code Playgroud)
在我的第一个控制器viewWillAppear我设置:
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = true
}
Run Code Online (Sandbox Code Playgroud)
在我的SECOND控制器中:
if let appDelegate = …Run Code Online (Sandbox Code Playgroud)