相关疑难解决方法(0)

在Swift iOS中设置设备方向

我正在开发一款适用于iPhone的快速应用程序.在我的应用程序中有一个模态视图,我只想在纵向视图中.

我的问题是,我如何以编程方式强制手机不允许旋转?换句话说,我正在寻找不允许以横向模式显示模态视图的代码(打开纵向旋转锁定).

这仅适用于1个模态视图,因此我无法关闭整个应用程序的旋转,否则我只会完全禁用旋转.

我在这里的研究中找到了代码 但是在目标C中,如果有帮助的话.谢谢!

rotation swift ios8

63
推荐指数
9
解决办法
10万
查看次数

如何使用swift仅锁定主视图的纵向方向

我使用swift为iPhone创建了一个应用程序,它由嵌入在导航控制器中的许多视图组成.我想将主视图锁定为纵向方向,并且仅锁定横向方向的导航控制器的子视图.这是我的意思的一个例子:

  • UINavigationController的
    • UiViewController1(纵向锁定)初始视图控制器,导航栏上有一个按钮,可以让用户访问可以选择其他视图的列表
    • UIViewController2(景观锁定)
    • UiViewController3(人像与风景)
    • UiViewController4(人像与风景)
    • ...
    • ...

我怎样才能做到这一点?

ios swift

39
推荐指数
7
解决办法
5万
查看次数

如何在Swift中以编程方式旋转方向?

我尝试了下一个:

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)

orientation ios swift

8
推荐指数
3
解决办法
2万
查看次数

标签 统计

swift ×3

ios ×2

ios8 ×1

orientation ×1

rotation ×1