我在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));
答案 0 :(得分:1)
尝试使用
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);
}