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

Landscape detected while in Portrait mode (Simulator)

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

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

代码:

    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 中的复选框以启用倒置方向时似乎存在错误,因此您必须添加代码才能在视图控制器中执行此操作。

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