我正在尝试以编程方式确定我的应用程序的当前高度和宽度。我使用以下代码:
CGRect screenRect = [[UIScreen mainScreen] bounds];
但是这会产生一个宽度为320和高度为480的结果,无论设备是否处于纵向或横向方向。我该如何确定我的主屏幕的当前宽度和高度(即取决于设备方向)?
我正在尝试以编程方式确定我的应用程序的当前高度和宽度。我使用以下代码:
CGRect screenRect = [[UIScreen mainScreen] bounds];
但是这会产生一个宽度为320和高度为480的结果,无论设备是否处于纵向或横向方向。我该如何确定我的主屏幕的当前宽度和高度(即取决于设备方向)?
float msWidth = [[UIScreen mainScreen] bounds].size.width*(IS_RETINA?2.0f:1.0f);
float msHeight = [[UIScreen mainScreen] bounds].size.height*(IS_RETINA?2.0f:1.0f);
if ( UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ) {
os->setWidth(MIN(msWidth, msHeight));
os->setHeight(MAX(msWidth, msHeight));
} else {
os->setWidth(MAX(msWidth, msHeight));
os->setHeight(MIN(msWidth, msHeight));
}
NSLog(@"screen_w %f", os->getWidth());
NSLog(@"screen_h %f", os->getHeight());
然而,在iOS 8.0.2上:
+ (NSUInteger)currentWindowWidth
{
NSInteger width = 0;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
CGSize size = [UIScreen mainScreen].bounds.size;
// if (UIInterfaceOrientationIsLandscape(orientation)) {
// width = size.height;
// } else {
width = size.width;
// }
return width;
}