iOS 6 应用程序 - 如何处理 iPhone 5 的屏幕尺寸?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12396545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:05:16  来源:igfitidea点击:

iOS 6 apps - how to deal with iPhone 5 screen size?

iphoneiosiphone-5

提问by jturolla

Possible Duplicate:
How to develop or migrate apps for iPhone 5 screen resolution?

可能的重复:
如何为 iPhone 5 屏幕分辨率开发或迁移应用程序?

I was just wondering with how should we deal with the iPhone 5 bigger screen size.

我只是想知道我们应该如何处理 iPhone 5 更大的屏幕尺寸。

As it has more pixels in height, things like GCRectMake that use coordinates (and just doubled the pixels with the retina/non retina problem) won't work seamlessly between versions, as it happened when we got the Retina.

由于它有更多的像素高度,像 GCRectMake 这样使用坐标的东西(并且只是将像素与视网膜/非视网膜问题加倍)在版本之间无法无缝工作,就像我们获得Retina时发生的那样。

And will we have to design two storyboards, just like for the iPad?

我们是否必须像 iPad 一样设计两个故事板?

I personally don't think Apple will require you to check the screen size every time you have to draw something, like many answers say. Does that happen with the iPad?

我个人认为 Apple 不会要求您在每次必须绘制某些内容时检查屏幕尺寸,就像许多答案所说的那样。iPad会出现这种情况吗?

采纳答案by Anurag

All apps will continue to work in the vertically stretched screen from what I could tell in today's presentation. They will be letterboxed or basically the extra 88 points in height would simply be black.

从我今天的演示中可以看出,所有应用程序都将继续在垂直拉伸的屏幕上工作。它们将被装在信箱中,或者基本上额外的 88 点高度将只是黑色。

If you only plan to support iOS 6+, then definitely consider using Auto Layout. It removes all fixed layout handling and instead uses constraints to lay things out. Nothing will be hard-coded, and your life will become a lot simpler.

如果你只打算支持 iOS 6+,那么一定要考虑使用 Auto Layout。它删除了所有固定的布局处理,而是使用约束来布局。没有什么是硬编码的,你的生活会变得更简单。

However, if you have to support older iOS's, then it really depends on your application. A majority of applications that use a standard navigation bar, and/or tab bar, could simply expand the content in the middle to use up that extra points. Set the autoresizing mask of the center content to expand in both directions.

但是,如果您必须支持较旧的 iOS,那么这实际上取决于您的应用程序。大多数使用标准导航栏和/或标签栏的应用程序可以简单地扩展中间的内容以用完这些额外的点。设置中心内容的自动调整大小蒙版以在两个方向上扩展。

view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

It works great out of the box for table views, however, if your app used pixel-perfect layout for displaying content, then your best bet would be to re-imagine the content so that it can accommodate varying heights.

对于表格视图,它开箱即用,效果很好,但是,如果您的应用程序使用像素完美的布局来显示内容,那么最好的办法是重新构想内容,以便它可以适应不同的高度。

If that's not a possibility, then the only remaining option is to have two UIs (pre iPhone 5, and iPhone 5).

如果这不可能,那么剩下的唯一选择就是拥有两个 UI(iPhone 5 之前和 iPhone 5)。

If that sounds ugly, then you could go with the default letterboxed model where the extra points/pixels just show up black.

如果这听起来很难看,那么您可以使用默认的信箱模型,其中额外的点/像素仅显示为黑色。

Edit

编辑

To enable your apps to work with iPhone 5, you need to add a retina version of the launcher image. It should be named [email protected]. And it has to be retina quality - there's no backward compatibility here :)

要使您的应用程序能够与 iPhone 5 配合使用,您需要添加启动器图像的视网膜版本。它应该被命名为[email protected]。而且它必须是视网膜质量 - 这里没有向后兼容性:)

You could also select this image from within Xcode. Go to the target, and under the Summary section, look for Launch Images. The image has to be 640x1136 pixels in size. Here's a screenshot of where to find it, if that helps.

您也可以从 Xcode 中选择此图像。转到目标,然后在摘要部分下,查找启动图像。图像大小必须为 640x1136 像素。如果有帮助,这是在哪里找到它的屏幕截图。

Xcode screenshot

Xcode 截图

回答by danielemm

You need to add a 640x1136 pixels PNGimage ([email protected]) as a 4 inch default splash image of your project, and it will use extra spaces (without efforts on simple table based applications, games will require more efforts).

您需要添加一个 640x1136 像素的PNG图像 ( [email protected]) 作为您项目的 4 英寸默认初始图像,并且它将使用额外的空间(无需在简单的基于表格的应用程序上工作,游戏将需要更多的工作)。

I've created a small UIDevice category in order to deal with all screen resolutions. You can get it here, but the code is as follows:

我创建了一个小的 UIDevice 类别来处理所有屏幕分辨率。你可以在这里得到它,但代码如下:

File UIDevice+Resolutions.h:

文件UIDevice+Resolutions.h

enum {
    UIDeviceResolution_Unknown           = 0,
    UIDeviceResolution_iPhoneStandard    = 1,    // iPhone 1,3,3GS Standard Display  (320x480px)
    UIDeviceResolution_iPhoneRetina4    = 2,    // iPhone 4,4S Retina Display 3.5"  (640x960px)
    UIDeviceResolution_iPhoneRetina5     = 3,    // iPhone 5 Retina Display 4"       (640x1136px)
    UIDeviceResolution_iPadStandard      = 4,    // iPad 1,2,mini Standard Display   (1024x768px)
    UIDeviceResolution_iPadRetina        = 5     // iPad 3 Retina Display            (2048x1536px)
}; typedef NSUInteger UIDeviceResolution;

@interface UIDevice (Resolutions)

- (UIDeviceResolution)resolution;

NSString *NSStringFromResolution(UIDeviceResolution resolution);

@end

File UIDevice+Resolutions.m:

文件UIDevice+Resolutions.m

#import "UIDevice+Resolutions.h"

@implementation UIDevice (Resolutions)

- (UIDeviceResolution)resolution
{
    UIDeviceResolution resolution = UIDeviceResolution_Unknown;
    UIScreen *mainScreen = [UIScreen mainScreen];
    CGFloat scale = ([mainScreen respondsToSelector:@selector(scale)] ? mainScreen.scale : 1.0f);
    CGFloat pixelHeight = (CGRectGetHeight(mainScreen.bounds) * scale);

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
        if (scale == 2.0f) {
            if (pixelHeight == 960.0f)
                resolution = UIDeviceResolution_iPhoneRetina4;
            else if (pixelHeight == 1136.0f)
                resolution = UIDeviceResolution_iPhoneRetina5;

        } else if (scale == 1.0f && pixelHeight == 480.0f)
            resolution = UIDeviceResolution_iPhoneStandard;

    } else {
        if (scale == 2.0f && pixelHeight == 2048.0f) {
            resolution = UIDeviceResolution_iPadRetina;

        } else if (scale == 1.0f && pixelHeight == 1024.0f) {
            resolution = UIDeviceResolution_iPadStandard;
        }
    }

    return resolution;
 }

 @end

This is how you need to use this code.

这就是您需要使用此代码的方式。

1) Add the above UIDevice+Resolutions.h & UIDevice+Resolutions.m files to your project

1) 将上述 UIDevice+Resolutions.h & UIDevice+Resolutions.m 文件添加到你的项目中

2) Add the line #import "UIDevice+Resolutions.h" to your ViewController.m

2) 将 #import "UIDevice+Resolutions.h" 行添加到您的 ViewController.m

3) Add this code to check what versions of device you are dealing with

3)添加此代码以检查您正在处理的设备版本

int valueDevice = [[UIDevice currentDevice] resolution];

    NSLog(@"valueDevice: %d ...", valueDevice);

    if (valueDevice == 0)
    {
        //unknow device - you got me!
    }
    else if (valueDevice == 1)
    {
        //standard iphone 3GS and lower
    }
    else if (valueDevice == 2)
    {
        //iphone 4 & 4S
    }
    else if (valueDevice == 3)
    {
        //iphone 5
    }
    else if (valueDevice == 4)
    {
        //ipad 2
    }
    else if (valueDevice == 5)
    {
        //ipad 3 - retina display
    }

回答by Sverrisson

I have just finished updating and sending an iOS 6.0 version of one of my Apps to the store. This version is backwards compatible with iOS 5.0, thus I kept the shouldAutorotateToInterfaceOrientation:method and added the new ones as listed below.

我刚刚完成更新并将我的一个应用程序的 iOS 6.0 版本发送到商店。此版本向后兼容 iOS 5.0,因此我保留了该shouldAutorotateToInterfaceOrientation:方法并添加了如下所列的新方法。

I had to do the following:

我必须执行以下操作:

Autorotation is changing in iOS 6. In iOS 6, the shouldAutorotateToInterfaceOrientation:method of UIViewController is deprecated. In its place, you should use the supportedInterfaceOrientationsForWindow:and shouldAutorotatemethods. Thus, I added these new methods (and kept the old for iOS 5 compatibility):

Autorotation 在 iOS 6 中发生了变化。在 iOS 6 中,shouldAutorotateToInterfaceOrientation:不推荐使用 UIViewController的方法。取而代之的是,您应该使用supportedInterfaceOrientationsForWindow:shouldAutorotate方法。因此,我添加这些新方法(并保持旧的iOS 5兼容性):

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;    
}
  • Used the view controller's viewWillLayoutSubviewsmethod and adjust the layout using the view's bounds rectangle.
  • Modal view controllers:The willRotateToInterfaceOrientation:duration:,
    willAnimateRotationToInterfaceOrientation:duration:, and
    didRotateFromInterfaceOrientation:methods are no longer called on any view controller that makes a full-screen presentation over
    itself
    —for example, presentViewController:animated:completion:.
  • Then I fixed the autolayout for views that needed it.
  • Copied images from the simulator for startup view and views for the iTunes store into PhotoShop and exported them as png files.
  • The name of the default image is: [email protected]and the size is 640×1136. It′s also allowed to supply 640×1096 for the same portrait mode (Statusbar removed). Similar sizes may also be supplied in landscape mode if your app only allows landscape orientation on the iPhone.
  • I have dropped backward compatibility for iOS 4. The main reason for that is because support for armv6code has been dropped. Thus, all devices that I am able to support now (running armv7) can be upgraded to iOS 5.
  • I am also generation armv7s code to support the iPhone 5 and thus can not use any third party frameworks (as Admob etc.) until they are updated.
  • 使用视图控制器的viewWillLayoutSubviews方法并使用视图的边界矩形调整布局。
  • 模态视图控制器:所述的willRotateToInterfaceOrientation:duration:
    willAnimateRotationToInterfaceOrientation:duration:以及
    didRotateFromInterfaceOrientation:方法不再调用的任何视图控制器上,使一个全屏幕呈现在
    本身
    -例如,presentViewController:animated:completion:
  • 然后我修复了需要它的视图的自动布局。
  • 将模拟器中用于启动视图和 iTunes 商店视图的图像复制到 PhotoShop 中,并将它们导出为 png 文件。
  • 默认图片名称为:[email protected],大小为 640×1136。它还允许为相同的纵向模式提供 640×1096(删除状态栏)。如果您的应用程序仅允许在 iPhone 上横向显示,也可以在横向模式下提供类似的尺寸。
  • 我已经放弃了对 iOS 4 的向后兼容性。主要原因是因为armv6已经放弃了对代码的支持。因此,我现在能够支持(运行armv7)的所有设备都可以升级到 iOS 5。
  • 我也在生成 armv7s 代码来支持 iPhone 5,因此在更新之前不能使用任何第三方框架(如 Admob 等)。

That was all but just remember to test the autorotation in iOS 5 and iOS 6 because of the changes in rotation.

由于旋转的变化,请记住在 iOS 5 和 iOS 6 中测试自动旋转。

回答by user1406691

No.

不。

if ([[UIScreen mainScreen] bounds].size.height > 960)

on iPhone 5 is wrong

在 iPhone 5 上是错误的

if ([[UIScreen mainScreen] bounds].size.height == 568)

回答by endy

@interface UIDevice (Screen)
typedef enum
{
    iPhone          = 1 << 1,
    iPhoneRetina    = 1 << 2,
    iPhone5         = 1 << 3,
    iPad            = 1 << 4,
    iPadRetina      = 1 << 5

} DeviceType;

+ (DeviceType)deviceType;
@end

.m

.m

#import "UIDevice+Screen.h"
@implementation UIDevice (Screen)

+ (DeviceType)deviceType
{
    DeviceType thisDevice = 0;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    {
        thisDevice |= iPhone;
        if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
        {
            thisDevice |= iPhoneRetina;
            if ([[UIScreen mainScreen] bounds].size.height == 568)
                thisDevice |= iPhone5;
        }
    }
    else
    {
        thisDevice |= iPad;
        if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
            thisDevice |= iPadRetina;
    }
    return thisDevice;
}

@end

This way, if you want to detect whether it is just an iPhone or iPad (regardless of screen-size), you just use:

这样,如果您想检测它是 iPhone 还是 iPad(无论屏幕大小),您只需使用:

if ([UIDevice deviceType] & iPhone) 

or

或者

if ([UIDevice deviceType] & iPad)

If you want to detect just the iPhone 5, you can use

如果你只想检测 iPhone 5,你可以使用

if ([UIDevice deviceType] & iPhone5)

As opposed to Malcoms answer where you would need to check just to figure out if it's an iPhone,

与 Malcoms 的回答相反,您需要检查以确定它是否是 iPhone,

if ([UIDevice currentResolution] == UIDevice_iPhoneHiRes || 
    [UIDevice currentResolution] == UIDevice_iPhoneStandardRes || 
    [UIDevice currentResolution] == UIDevice_iPhoneTallerHiRes)`

Neither way has a major advantage over one another, it is just a personal preference.

这两种方式都没有彼此的主要优势,这只是个人偏好。

回答by Byron Rode

@Pascal's comment on the OP's question is right. By simply adding the image, it removes the black borders and the app will use the full height.

@Pascal 对 OP 问题的评论是正确的。通过简单地添加图像,它会删除黑色边框,应用程序将使用全高。

You will need to make adjustments to any CGRects by determining that the device is using the bigger display. I.e. If you need something aligned to the bottom of the screen.

您需要通过确定设备正在使用更大的显示器来对任何 CGRects 进行调整。即,如果您需要与屏幕底部对齐的内容。

I am sure there is a built in method, but I haven't seen anything and a lot is still under NDA so the method we use in our apps is quite simply a global function. Add the following to your .pch file and then its a simple if( is4InchRetina() ) { ... }call to make adjustments to your CGRects etc.

我确信有一个内置的方法,但我还没有看到任何东西,而且很多仍然处于保密协议之下,所以我们在我们的应用程序中使用的方法只是一个全局函数。将以下内容添加到您的 .pch 文件中,然后通过一个简单的if( is4InchRetina() ) { ... }调用来调整您的 CGRects 等。

static BOOL is4InchRetina()
{
    if (![UIApplication sharedApplication].statusBarHidden && (int)[[UIScreen mainScreen] applicationFrame].size.height == 548 || [UIApplication sharedApplication].statusBarHidden && (int)[[UIScreen mainScreen] applicationFrame].size.height == 568)
        return YES;

    return NO;
}

回答by Tomasz Szulc

I think you can use [UIScreen mainScreen].bounds.size.heightand calculate step for your objects. when you calculate step you can set coordinates for two resolutions.

我认为您可以[UIScreen mainScreen].bounds.size.height为您的对象使用和计算步骤。当您计算步骤时,您可以为两种分辨率设置坐标。

Or you can get height like above and if(iphone5) then... else if(iphone4) then... else if(ipad). Something like this.

或者你可以得到像上面和if(iphone5) then... else if(iphone4) then... else if(ipad). 像这样的东西。

If you use storyboards then you have to create new for new iPhone i think.

如果您使用故事板,那么我认为您必须为新 iPhone 创建新的。

回答by Tomasz Szulc

As it has more pixels in height, things like GCRectMake that use coordinates won't work seamlessly between versions, as it happened when we got the Retina.

因为它有更多的像素高度,像 GCRectMake 这样使用坐标的东西不能在版本之间无缝地工作,就像我们得到 Retina 时发生的那样。

Well, they dowork the same with Retina displays - it's just that 1 unit in the CoreGraphics coordinate system will correspond to 2 physical pixels, but you don't/didn't have to do anything, the logic stayed the same. (Have you actually tried to run one of your non-retina apps on a retina iPhone, ever?)

那么,他们的工作一样具有Retina显示屏-它只是1个单位在CoreGraphics中的坐标系将对应于2个物理像素,但你不/没有做任何事情,逻辑保持不变。(您是否真的尝试过在 Retina iPhone 上运行您的非视网膜应用程序之一?

For the actual question: that's why you shouldn't use explicit CGRectMakes and co... That's why you have stuff like [[UIScreen mainScreen] applicationFrame].

对于实际问题:这就是为什么你不应该使用显式 CGRectMakes 和 co...这就是为什么你有像[[UIScreen mainScreen] applicationFrame].