ios - 在人像模式下检测到风景(模拟器)

标签 ios swift ios-simulator

我在 IOS 模拟器中启动了一个应用程序,我的应用程序认为设备处于横向模式,而设备处于纵向模式。

任何提示,为什么会发生这种情况以及如何正确检测肖像?

enter image description here

代码:

    let raworientation=UIDevice.currentDevice().orientation.rawValue;

    if raworientation==UIInterfaceOrientation.PortraitUpsideDown.rawValue {
        return "PortraitUpsideDown";
    }else if raworientation==UIInterfaceOrientation.Portrait.rawValue {
        return "Portrait";
    }else if raworientation==UIInterfaceOrientation.LandscapeLeft.rawValue {
        return "LandscapeLeft"; // IT GOES HERE !!!!, while the device is in portrait !!!!
    }else if raworientation==UIInterfaceOrientation.LandscapeRight.rawValue {
        return "LandscapeRight";
    }else{
        return "unknown";
    }

更新 另一件事似乎也是错误的。 在人像倒置的情况下,宽度和高度测量也是错误的:

let screenSize: CGRect = UIScreen.mainScreen().bounds;
print("W:" + String(screenSize.width) + "H:" + String(screenSize.height));

最佳答案

尝试使用

let raworientation = UIApplication.sharedApplication().statusBarOrientation

获取UI的方向

我在模拟器中测试了这段代码,两种方式都适用于所有方向

    let raworientation = UIApplication.sharedApplication().statusBarOrientation;

    if raworientation==UIInterfaceOrientation.PortraitUpsideDown {
        println("PortraitUpsideDown");
    }else if raworientation==UIInterfaceOrientation.Portrait {
        println("Portrait");
    }else if raworientation==UIInterfaceOrientation.LandscapeLeft {
        println("LandscapeLeft"); 
    }else if raworientation==UIInterfaceOrientation.LandscapeRight {
        println("LandscapeRight");
    }else{
        println("unknown");
    }

其他方法

override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
    switch (toInterfaceOrientation)
    {
        case (.PortraitUpsideDown): println("PortraitUpsideDown");
        case (.Portrait): println("Portrait");
        case (.LandscapeLeft): println("LandscapeLeft");
        case (.LandscapeRight): println("LandscapeRight");
        default: println("unknown");
    }

}

编辑: 当您只需单击 Xcode 中的复选框以启用倒置方向时,似乎存在一个错误,因此您必须添加代码才能在 View Controller 中执行此操作。

override func supportedInterfaceOrientations() -> Int {
    return Int(UIInterfaceOrientationMask.All.rawValue);
}

关于ios - 在人像模式下检测到风景(模拟器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35069420/

相关文章:

ios - 通过从应用程序图标启动,在应用程序完全关闭时处理远程通知

ios - 运行 ntimer 来显示广告,但仅显示在某些 View Controller 上

ios - URLQueryItem 值是可选的

iphone - 将图像添加到 iPhone 模拟器相册 - 已接受 "tricks"不起作用

ios - iOS 应用程序可以分配多少内存?

Xcode 8 : The request to open bundle-id failed. SpringBoard 无法为请求提供服务

ios - 如何在 Xcode Storyboard中定位警告?

iphone - 如果 20 分钟没有触摸应用程序,则注销用户

工具栏中条形按钮项底部的 Swift 条

swift - 在 Swift 中声明一个数组