I have an app started in the IOS Simulator and my app thinks the device is in Landscape mode while the device is in portrait mode.
我在IOS模拟器中启动了一个应用程序,我的应用程序认为设备处于纵向模式时设备处于横向模式。
Any hints, why this happen and how to detect the portrait correctly ?
任何提示,为什么会发生这种情况以及如何正确检测肖像?
Code:
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";
}
UPDATE Another thing which seems to be wrong too. In case of Portrait upside down, the width and height measures are wrong too:
更新另一件似乎也是错误的事情。如果纵向颠倒,宽度和高度测量也是错误的:
let screenSize: CGRect = UIScreen.mainScreen().bounds;
print("W:" + String(screenSize.width) + "H:" + String(screenSize.height));
1 个解决方案
#1
1
Try using
let raworientation = UIApplication.sharedApplication().statusBarOrientation
to get the orientation of the UI
获得UI的方向
I tested this code in the simulator and both ways work for all orientations
我在模拟器中测试了这段代码,两种方式都适用于所有方向
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");
}
other method
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");
}
}
Edit: There seems to be a bug when you just click the checkbox in Xcode to enable upside down orientation, so you have to add code to do it in your view controller.
编辑:当您单击Xcode中的复选框以启用颠倒方向时,似乎存在一个错误,因此您必须添加代码才能在视图控制器中执行此操作。
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.All.rawValue);
}
#1
1
Try using
let raworientation = UIApplication.sharedApplication().statusBarOrientation
to get the orientation of the UI
获得UI的方向
I tested this code in the simulator and both ways work for all orientations
我在模拟器中测试了这段代码,两种方式都适用于所有方向
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");
}
other method
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");
}
}
Edit: There seems to be a bug when you just click the checkbox in Xcode to enable upside down orientation, so you have to add code to do it in your view controller.
编辑:当您单击Xcode中的复选框以启用颠倒方向时,似乎存在一个错误,因此您必须添加代码才能在视图控制器中执行此操作。
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.All.rawValue);
}