我在 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 中的复选框以启用倒置方向时,似乎存在一个错误,因此您必须添加代码才能在 View Controller 中执行此操作。
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.All.rawValue);
}
关于ios - 在人像模式下检测到风景(模拟器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35069420/