如何获取屏幕高度和宽度的方向相关值?

97

我正在尝试以编程方式确定我的应用程序的当前高度和宽度。我使用以下代码:

CGRect screenRect = [[UIScreen mainScreen] bounds];

但是这会产生一个宽度为320和高度为480的结果,无论设备是否处于纵向或横向方向。我该如何确定我的主屏幕的当前宽度和高度(即取决于设备方向)?

12个回答

0
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());

0

然而,在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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接