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

jtu*_*lla 264 iphone ios iphone-5

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

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

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

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

我个人认为Apple不会要求你每次必须画一些东西来检查屏幕大小,就像许多答案所说的那样.这会发生在iPad上吗?

Anu*_*rag 209

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

如果您只打算支持iOS 6+,那么一定要考虑使用自动布局.它删除了所有固定的布局处理,而是使用约束来解决问题.没有什么是硬编码的,你的生活将变得更加简单.

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

view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Run Code Online (Sandbox Code Playgroud)

它可以很好地开箱即用于桌面视图,但是,如果您的应用使用像素完美的布局来显示内容,那么您最好的选择是重新设想内容,以便它可以适应不同的高度.

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

如果这听起来很难看,那么你可以使用默认的letterboxed模型,其中额外的点/像素只显示黑色.

编辑

要使您的应用能够与iPhone 5配合使用,您需要添加启动器图像的视网膜版本.应该命名Default-568h@2x.png.它必须是视网膜质量 - 这里没有向后兼容性:)

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

Xcode截图

  • @DaGaMs或者你可以将`self.window.frame = [UIScreen mainScreen] .bounds;`添加到`-application:didFinishLaunchingWithOptions:`的顶部. (6认同)

dan*_*emm 84

您需要添加640x1136像素的PNG图像(Default-568h@2x.png)作为项目的4英寸默认启动图像,并且它将使用额外的空间(在基于简单表格的应用程序上不费力,游戏将需要更多努力).

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

文件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
Run Code Online (Sandbox Code Playgroud)

文件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
Run Code Online (Sandbox Code Playgroud)

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

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

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

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
    }
Run Code Online (Sandbox Code Playgroud)


Han*_*son 56

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

我必须做以下事情:

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

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;    
}
Run Code Online (Sandbox Code Playgroud)
  • 使用视图控制器的viewWillLayoutSubviews方法并使用视图的边界矩形调整布局.
  • 模态视图控制器:所述的willRotateToInterfaceOrientation:duration:,
    willAnimateRotationToInterfaceOrientation:duration:以及
    didRotateFromInterfaceOrientation:方法不再调用的任何视图控制器上,使一个全屏幕呈现在
    本身
    -例如,presentViewController:animated:completion:.
  • 然后我修复了需要它的视图的自动布局.
  • 将模拟器中的图像复制到iTunes商店的启动视图和视图,并将其导出为png文件.
  • 默认图像的名称是:Default-568h@2x.png并且大小为640×1136.它也允许为相同的肖像模式提供640×1096(状态栏已删除).如果您的应用仅允许iPhone上的横向显示,也可以在横向模式下提供相似的尺寸.
  • 我已经放弃了iOS 4的向后兼容性.主要原因是因为armv6代码的支持已被删除.因此,我现在能够支持的所有设备(正在运行armv7)都可以升级到iOS 5.
  • 我也是生成armv7s代码以支持iPhone 5,因此在更新之前不能使用任何第三方框架(如Admob等).

这只是记得在iOS 5和iOS 6中测试自动旋转因为旋转的变化.

  • 我的应用程序已经有一个可调整大小的视图,但仍然在信箱中启动.即使我将视图的宽度更改为568,它仍然会启动letterboxed(并且压缩,启动)!一旦我添加了Default-568h@2x.png,就会以适当的宽高比启动它. (2认同)
  • andrewx,是的.你支持旧的(保留在以前的方法中)并添加上面的新方法.然后,当运行iOS 5时,它将调用旧的,iOS 6调用新的. (2认同)

use*_*691 26

没有.

if ([[UIScreen mainScreen] bounds].size.height > 960)
Run Code Online (Sandbox Code Playgroud)

在iPhone 5上是错误的

if ([[UIScreen mainScreen] bounds].size.height == 568)
Run Code Online (Sandbox Code Playgroud)

  • iPhone 5的屏幕分辨率为1136x640; 然而,所有尺寸均以2像素为单位,与原始iPhone屏幕保持一致 - Apple将iPhone 4中每个方向的像素数量增加了一倍,但希望所有软件都能保持不变.所以568是新屏幕的高度(= 1136/2). (6认同)
  • 为什么568 ?? 请解释一下这个.谢谢 (2认同)

end*_*ndy 17

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

} DeviceType;

+ (DeviceType)deviceType;
@end
Run Code Online (Sandbox Code Playgroud)

.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
Run Code Online (Sandbox Code Playgroud)

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

if ([UIDevice deviceType] & iPhone) 
Run Code Online (Sandbox Code Playgroud)

要么

if ([UIDevice deviceType] & iPad)
Run Code Online (Sandbox Code Playgroud)

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

if ([UIDevice deviceType] & iPhone5)
Run Code Online (Sandbox Code Playgroud)

与Malcoms的答案相反,你需要检查一下,看看它是不是iPhone,

if ([UIDevice currentResolution] == UIDevice_iPhoneHiRes || 
    [UIDevice currentResolution] == UIDevice_iPhoneStandardRes || 
    [UIDevice currentResolution] == UIDevice_iPhoneTallerHiRes)`
Run Code Online (Sandbox Code Playgroud)

这两种方式都没有相互优势,这只是个人偏好.


Byr*_*ode 5

@Pascal对OP问题的评论是正确的.只需添加图像,它就会删除黑色边框,应用程序将使用全高.

您需要通过确定设备使用更大的显示器来对任何CGRect进行调整.即如果你需要一些与屏幕底部对齐的东西.

我确信有一个内置的方法,但我还没有看到任何东西,而且很多仍然在NDA下,所以我们在我们的应用程序中使用的方法非常简单的全局功能.将以下内容添加到.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;
}
Run Code Online (Sandbox Code Playgroud)