简体   繁体   English

iPad2仅给出错误的宽度和高度?

[英]iPad2 only gives wrong width and height?

on any other device we use this code to set all views in landscape and everything fits great : 在任何其他设备上,我们使用此代码将所有视图设置为横向,一切都非常适合:

float width=[UIScreen mainScreen].bounds.size.width;
float height= [UIScreen mainScreen].bounds.size.height;

on the iPad 2 only , in order to make it work on landscape we have to swap the width and height , otherwise he puts views on portrait and they seems ugly. only在iPad 2上,为了使其能够在横向上使用,我们必须交换width and height ,否则他将视图放在肖像上,它们看起来很难看。

Why is it happens only in iPad2 ? 为什么仅在iPad2中会发生这种情况?

It's not linked to the device but to iOS. 它不链接到设备,而是链接到iOS。 Since iOS 8.0, the bounds is now dependent of the device orientation. 从iOS 8.0开始,边界现在取决于设备方向。 I'm also swapping width and height like this : 我也像这样交换宽度和高度:

CGRect ScreenBounds() { CGRect bounds = [UIScreen mainScreen].bounds; CGRect tmp_bounds = bounds; if(bounds.size.width < bounds.size.height) { bounds.size.width = tmp_bounds.size.height; bounds.size.height = tmp_bounds.size.width; } return bounds; }

Don't size your views relative to the screen, but relative to their container view. 不要相对于屏幕,而是相对于其容器视图调整视图的大小。 Simple as that. 就那么简单。

I had same issue From iOS 8 UIScreen is interface oriented so you will get proper results on devices which are running on iOS 8. 我从iOS 8遇到了同样的问题UIScreen是面向界面的,因此您将在运行iOS 8的设备上获得正确的结果。

In order to support iOS 7 as well you can use following util method: 为了也支持iOS 7,您可以使用以下util方法:

+ (CGSize)screenSize {
CGSize screenSize = [UIScreen mainScreen].bounds.size;
if ((NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
    return CGSizeMake(screenSize.height, screenSize.width);
}
return screenSize;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM