简体   繁体   English

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

[英]How to develop or migrate apps for iPhone 5 screen resolution?

The new iPhone 5 display has a new aspect ratio and a new resolution (640 x 1136 pixels).新的 iPhone 5 显示屏具有新的纵横比和新的分辨率(640 x 1136 像素)。

What is required to develop new or transition already existing applications to the new screen size?开发新应用程序或将现有应用程序转换为新的屏幕尺寸需要什么?

What should we keep in mind to make applications "universal" for both the older displays and the new widescreen aspect ratio?我们应该记住什么才能使应用程序“通用”适用于旧显示器和新宽屏纵横比?

  1. Download and install latest version of Xcode .下载并安装最新版本的Xcode
  2. Set a Launch Screen File for your app (in the general tab of your target settings).为您的应用程序设置启动屏幕文件(在目标设置的常规选项卡中)。 This is how you get to use the full size of any screen, including iPad split view sizes in iOS 9.这就是您如何使用任何屏幕的完整尺寸,包括 iOS 9 中的 iPad 拆分视图尺寸。
  3. Test your app, and hopefully do nothing else, since everything should work magically if you had set auto resizing masks properly, or used Auto Layout.测试您的应用程序,希望不要做其他事情,因为如果您正确设置了自动调整大小蒙版或使用了自动布局,一切都应该神奇地工作。
  4. If you didn't, adjust your view layouts, preferably with Auto Layout.如果没有,请调整您的视图布局,最好使用自动布局。
  5. If there is something you have to do for the larger screens specifically, then it looks like you have to check height of [[UIScreen mainScreen] bounds] as there seems to be no specific API for that.如果您必须专门针对较大的屏幕执行某些操作,那么您似乎必须检查[[UIScreen mainScreen] bounds]高度,因为似乎没有特定的 API。 As of iOS 8 there are also size classes that abstract screen sizes into regular or compact vertically and horizontally and are recommended way to adapt your UI.从 iOS 8 开始,还有一些尺寸类可以将屏幕尺寸抽象为垂直和水平方向的常规或紧凑尺寸,并且是调整 UI 的推荐方法。

If you have an app built for iPhone 4S or earlier, it'll run letterboxed on iPhone 5.如果您有为 iPhone 4S 或更早版本构建的应用程序,它将在 iPhone 5 上运行。

To adapt your app to the new taller screen, the first thing you do is to change the launch image to: Default-568h@2x.png.要使您的应用程序适应新的更高屏幕,您要做的第一件事是将启动图像更改为:Default-568h@2x.png。 Its size should be 1136x640 (HxW).其大小应为 1136x640 (HxW)。 Yep, having the default image in the new screen size is the key to let your app take the whole of new iPhone 5's screen .是的,在新的屏幕尺寸中使用默认图像是让您的应用程序占据整个新 iPhone 5 屏幕的关键

(Note that the naming convention works only for the default image. Naming another image "Image-568h@2x.png" will not cause it to be loaded in place of "Image@2x.png". If you need to load different images for different screen sizes, you'll have to do it programmatically.) (请注意,命名约定仅适用于默认图像。将另一个图像命名为“Image-568h@2x.png”不会导致它被加载到“Image@2x.png”的位置。如果您需要加载不同的图像对于不同的屏幕尺寸,您必须以编程方式进行。)

If you're very very lucky, that might be it... but in all likelihood, you'll have to take a few more steps.如果您非常幸运,那可能就是这样……但很可能,您必须多走几步。

  • Make sure, your Xibs/Views use auto-layout to resize themselves.确保您的 Xibs/Views 使用自动布局来调整自己的大小。
  • Use springs and struts to resize views.使用弹簧和支柱来调整视图的大小。
  • If this is not good enough for your app, design your xib/storyboard for one specific screen size and reposition programmatically for the other.如果这对您的应用程序来说还不够好,请为一种特定的屏幕尺寸设计您的 xib/故事板,并以编程方式为另一种屏幕重新定位。

In the extreme case (when none of the above suffices), design the two Xibs and load the appropriate one in the view controller.在极端情况下(当以上都不满足时),设计两个 Xib 并在视图控制器中加载适当的一个。

To detect screen size:检测屏幕尺寸:

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    CGSize result = [[UIScreen mainScreen] bounds].size;
    if(result.height == 480)
    {
        // iPhone Classic
    }
    if(result.height == 568)
    {
        // iPhone 5
    }
}

The only really required thing to do is to add a launch image named "Default-568h@2x.png" to the app resources, and in general case (if you're lucky enough) the app will work correctly.唯一真正需要做的是将名为“Default-568h@2x.png”的启动图像添加到应用程序资源中,并且在一般情况下(如果您足够幸运)应用程序将正常工作。

In case the app does not handle touch events, then make sure that the key window has the proper size.如果应用程序不处理触摸事件,请确保键窗口具有正确的大小。 The workaround is to set the proper frame:解决方法是设置适当的框架:

[window setFrame:[[UIScreen mainScreen] bounds]]

There are other issues not related to screen size when migrating to iOS 6. Read iOS 6.0 Release Notes for details.迁移到 iOS 6 时还有其他与屏幕尺寸无关的问题。请阅读iOS 6.0 发行说明了解详细信息。

Sometimes (for pre-storyboard apps), if the layout is going to be sufficiently different, it's worth specifying a different xib according to device (see this question - you'll need to modify the code to deal with iPhone 5) in the viewController init, as no amount of twiddling with autoresizing masks will work if you need different graphics.有时(对于 pre-storyboard 应用程序),如果布局将有足够的不同,则值得在 viewController 中根据设备指定不同的 xib(请参阅此问题- 您需要修改代码以处理 iPhone 5) init,因为如果您需要不同的图形,那么使用自动调整大小蒙版的任何操作都不起作用。

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    NSString *myNibName;
    if ([MyDeviceInfoUtility isiPhone5]) myNibName = @"MyNibIP5";
    else myNibName = @"MyNib";

    if ((self = [super initWithNibName:myNibName bundle:nibBundleOrNil])) {


...

This is useful for apps which are targeting older iOS versions.这对于针对较旧 iOS 版本的应用程序很有用。

Here you can find a nice tutorial (for MonoTouch, but you can use the information for Non-MonoTouch-projects, too):在这里你可以找到一个很好的教程(对于 MonoTouch,但你也可以使用非 MonoTouch 项目的信息):
http://redth.info/get-your-monotouch-apps-ready-for-iphone-5-ios-6-today/ http://redth.info/get-your-monotouch-apps-ready-for-iphone-5-ios-6-today/

  1. Create a new image for your splash/default screen ( 640 x 1136 pixel ) with the name " Default-568h@2x.png "为您的初始/默认屏幕( 640 x 1136 像素)创建一个名为“ Default-568h@2x.png ”的新图像

  2. In the iOS Simulator , go to the Hardware -> Device menu, and select " iPhone (Retina 4-inch) "iOS Simulator 中,转到 Hardware -> Device 菜单,然后选择“ iPhone (Retina 4-inch)

  3. Create other images, eg background images创建其他图像,例如背景图像

  4. Detect iPhone 5 to load your new images:检测 iPhone 5 以加载您的新图像:

public static bool IsTall
{
    get {
        return UIDevice.currentDevice.userInterfaceIdiom
                    == UIUserInterfaceIdiomPhone
                && UIScreen.mainScreen.bounds.size.height
                    * UIScreen.mainScreen.scale >= 1136;
    }
}

private static string tallMagic = "-568h@2x";
public static UIImage FromBundle16x9(string path)
{
    //adopt the -568h@2x naming convention
    if(IsTall())
    {
        var imagePath = Path.GetDirectoryName(path.ToString());
        var imageFile = Path.GetFileNameWithoutExtension(path.ToString());
        var imageExt = Path.GetExtension(path.ToString());
        imageFile = imageFile + tallMagic + imageExt;
        return UIImage.FromFile(Path.Combine(imagePath,imageFile));
    }
    else
    {
        return UIImage.FromBundle(path.ToString());
    }
}

It's easy for migrating iPhone5 and iPhone4 through XIBs.........通过 XIB 迁移 iPhone5 和 iPhone4 很容易.......

UIViewController *viewController3;
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
    UIViewController *viewController3 = [[[mainscreenview alloc] initWithNibName:@"iphone5screen" bundle:nil] autorelease];               
}    
else
{
     UIViewController *viewController3 = [[[mainscreenview alloc] initWithNibName:@"iphone4screen" bundle:nil] autorelease];
}

I solve this problem here .在这里解决了这个问题。 Just add ~568h@2x suffix to images and ~568h to xib's.只需在图像中添加 ~568h@2x 后缀,将 ~568h 添加到 xib 即可。 No needs more runtime checks or code changes.不需要更多的运行时检查或代码更改。

I had added the new default launch image and (in checking out several other SE answers...) made sure my storyboards all auto-sized themselves and subviews but the retina 4 inches still letterboxed.我添加了新的默认启动图像,并且(在查看其他几个 SE 答案时...)确保我的故事板都自动调整大小和子视图,但视网膜 4 英寸仍然是信箱。

Then I noticed that my info plist had a line item for " Launch image " set to " Default.png ", which I thusly removed and magically letterboxing no longer appeared.然后我注意到我的信息 plist 有一个“启动图像”的行项设置为“ Default.png ”,因此我删除了它并且不再出现神奇的信箱。 Hopefully, that saves someone else the same craziness I endured.希望这能让其他人像我所忍受的一样疯狂。

To determine if your app can support iPhone 5 Retina use this: (This could be more robust to return the type of display, 4S Retina, etc., but as it is written below, it just returns if the iPhone supports iOS5 Retina as a YES or NO)要确定您的应用程序是否可以支持 iPhone 5 Retina,请使用:(返回显示类型、4S Retina 等可能更健壮,但如下所述,如果 iPhone 支持 iOS5 Retina 作为是还是不是)

In a common ".h" file add:在常见的“.h”文件中添加:

BOOL IS_IPHONE5_RETINA(void);

In a common ".m" file add:在常见的“.m”文件中添加:

BOOL IS_IPHONE5_RETINA(void) {
    BOOL isiPhone5Retina = NO;
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        if ([UIScreen mainScreen].scale == 2.0f) {
            CGSize result = [[UIScreen mainScreen] bounds].size;
            CGFloat scale = [UIScreen mainScreen].scale;
            result = CGSizeMake(result.width * scale, result.height * scale);

            if(result.height == 960){
                //NSLog(@"iPhone 4, 4s Retina Resolution");
            }
            if(result.height == 1136){
                //NSLog(@"iPhone 5 Resolution");
                isiPhone5Retina = YES;
            }
        } else {
            //NSLog(@"iPhone Standard Resolution");
        }
    }
    return isiPhone5Retina;
}

In a constants.h file you can add these define statements:constants.h文件中,您可以添加以下定义语句:

 #define IS_IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad 
 #define IS_IPHONE UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone
 #define IS_WIDESCREEN (fabs((double)[[UIScreen mainScreen] bounds].size.height - (double)568) < DBL_EPSILON) 
 #define IS_IPHONE_5 (!IS_IPAD && IS_WIDESCREEN)

I guess, it is not going to work in all cases, but in my particular project it avoided me from duplication of NIB-files:我想,它不会在所有情况下都有效,但在我的特定项目中,它避免了我重复 NIB 文件:

Somewhere in common.h you can make these defines based off of screen height:common.h某个地方,您可以根据屏幕高度进行这些定义:

#define HEIGHT_IPHONE_5 568
#define IS_IPHONE   ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 ([[UIScreen mainScreen] bounds ].size.height == HEIGHT_IPHONE_5)

In your base controller:在您的基本控制器中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (IS_IPHONE_5) {
        CGRect r = self.view.frame;
        r.size.height = HEIGHT_IPHONE_5 - 20;
        self.view.frame = r;
    }
    // now the view is stretched properly and not pushed to the bottom
    // it is pushed to the top instead...

    // other code goes here...
}

First of all create two xibs and attach all delegates,main class to the xib and then u can put in this condition mentioned below in your appdelegate.m file in首先创建两个xib并将所有委托,主类附加到xib然后你可以在你的appdelegate.m文件中放入下面提到的这个条件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

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

        self.ViewController = [[ViewController alloc] initWithNibName:@"ViewControlleriphone5" bundle:nil];
        }

        else
        {
             self.ViewController = [[ViewController alloc] initWithNibName:@"ViewControlleriphone4" bundle:nil];

        }

you can use it any where in the program depending upon your requirements even in your ViewController classes.您可以根据您的要求在程序中的任何位置使用它,甚至在您的ViewController类中。 What matters the most is that you have created two xib files separate for iphone 4(320*480) and iphone 5(320*568)最重要的是您为iphone 4(320*480) and iphone 5(320*568)创建了两个单独的xib文件

Checking bounds with 568 will fail in landscape mode.在横向模式下使用568检查bounds将失败。 iPhone 5 launches only in portrait mode but if you want to support rotations then the iPhone 5 "check" will need to handle this scenario as well. iPhone 5 仅在纵向模式下启动,但如果您想支持旋转,那么 iPhone 5 的“检查”也需要处理这种情况。

Here's a macro which handles orientation state:这是一个处理方向状态的宏:

#define IS_IPHONE_5 (CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size, CGSizeMake(640, 1136)))

The use of the 'preferredMode' call is from another posting I read a few hours ago so I did not come up with this idea. 'preferredMode' 调用的使用来自我几个小时前阅读的另一篇文章,所以我没有想出这个想法。

您可以使用Auto Layout功能并使用 iPhone 5 屏幕分辨率创建设计,它适用于 4" 和 3.5" 设备,但在这种情况下,您应该对布局管理器有足够的了解。

Try the below method in a singleton class:在单例类中尝试以下方法:

-(NSString *)typeOfDevice
    {
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {
            CGSize result = [[UIScreen mainScreen] bounds].size;
            if(result.height == 480)
            {
                return @"Iphone";
            }
            if(result.height == 568)
            {
                return @"Iphone 5";
            }
        }
        else{
            return @"Ipad";;
        }


        return @"Iphone";
    }

I never faced such an issue with any device as I've had one codebase for all, without any hardcoded values.我从来没有在任何设备上遇到过这样的问题,因为我有一个适用于所有人的代码库,没有任何硬编码值。 What I do is to have the maximum sized image as resource instead of one for each device.我所做的是将最大尺寸的图像作为资源而不是每个设备一个。 For example, I would have one for retina display and show it as aspect fit so it will be views as is on every device.例如,我将有一个用于 Retina 显示器并将其显示为宽高比,因此它将在每个设备上显示为原样。 Coming to deciding the frame of button, for instance, at run time.例如,在运行时决定按钮的框架。 For this I use the % value of the patent view, example , if I want the width to be half of parent view take 50 % of parent and same applies for height and center.为此,我使用专利视图的 % 值,例如,如果我希望宽度为父视图的一半,则取父视图的 50%,同样适用于高度和中心。

With this I don't even need the xibs.有了这个,我什至不需要xibs。

First show this image.先展示这张图。 In that image you show warning for Retina 4 support so click on this warning and click on add so your Retina 4 splash screen automatically add in your project.在该图像中,您显示了对 Retina 4 支持的警告,因此单击此警告并单击添加,以便您的 Retina 4 启动画面自动添加到您的项目中。

在此处显示图像

and after you use this code :使用此代码后:

if([[UIScreen mainScreen] bounds].size.height == 568)
    {
        // For iphone 5
    }
    else
    {
        // For iphone 4 or less
    }

You can use this define to calculate if you are using the iPhone 5 based on screen size:您可以使用此定义根据屏幕尺寸计算您是否使用 iPhone 5:

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

then use a simple if statement :然后使用一个简单的if语句:

    if (IS_IPHONE_5) {

    // What ever changes
    }

Peter, you should really take a look at Canappi, it does all that for you, all you have to do is specify the layout as such:彼得,你真的应该看看 Canappi,它为你做了所有的事情,你所要做的就是指定布局:

button mySubmitButton 'Sumbit' (100,100,100,30 + 0,88,0,0) { ... }

From there Canappi will generate the correct objective-c code that detects the device the app is running on and will use:从那里 Canappi 将生成正确的objective-c 代码,用于检测应用程序正在运行的设备并将使用:

(100,100,100,30) for iPhone4
(100,**188**,100,30) for iPhone 5

Canappi works like Interface Builder and Story Board combined, except that it is in a textual form. Canappi 的工作方式类似于 Interface Builder 和 Story Board 的组合,只是它是文本形式。 If you already have XIB files, you can convert them so you don't have to recreate the entire UI from scratch.如果您已经有 XIB 文件,您可以转换它们,这样您就不必从头开始重新创建整个 UI。

You can manually check the screen size to determine which device you're on:您可以手动检查屏幕尺寸以确定您使用的设备:

#define DEVICE_IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.height == 568)

float height = DEVICE_IS_IPHONE5?568:480;
if (height == 568) {
    // 4"

} else {

    // 3"

}

You could add this code:您可以添加以下代码:

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
        if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)]) {
            CGSize result = [[UIScreen mainScreen] bounds].size;
            CGFloat scale = [UIScreen mainScreen].scale;
            result = CGSizeMake(result.width * scale, result.height * scale);

            if(result.height == 960) {
                NSLog(@"iPhone 4 Resolution");
            }
            if(result.height == 1136) {
              NSLog(@"iPhone 5 Resolution");
            }
        }
        else{
            NSLog(@"Standard Resolution");
        }
    }

This is a real universal code, you can create 3 different story board:这是一个真正的通用代码,您可以创建 3 个不同的故事板:

Set your project Universal mode, and set your main story iPhone with the iPhone5 storyboard and the ipad main with iPad target storyboard, now add new storyboard target for iphone and modify the resolution for iphone 4s or less now implement your AppDelegate.m设置您的项目通用模式,并设置您的主要故事 iPhone 与 iPhone5 故事板和 iPad 主与 iPad 目标故事板,现在为 iphone 添加新的故事板目标并修改 iphone 4s 或更少的分辨率现在实现您的AppDelegate.m

iPhone4/4s (is the same for 3/3Gs) one for iPhone5 and make the project universal , with a new Storyboard target for iPad, now in to AppDelegate.m under the didFinishLaunching add this code: iPhone4/4s (3/3Gs 相同) iPhone5 的一个,使项目通用,有了 iPad 的新 Storyboard 目标,现在在AppDelegate.m下的didFinishLaunching添加以下代码:

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
        UIStoryboard *storyBoard;

        CGSize result = [[UIScreen mainScreen] bounds].size;
        CGFloat scale = [UIScreen mainScreen].scale;
        result = CGSizeMake(result.width *scale, result.height *scale);

//----------------HERE WE SETUP FOR IPHONE4/4s/iPod----------------------

        if(result.height == 960){
            storyBoard = [UIStoryboard storyboardWithName:@"iPhone4_Storyboard" bundle:nil];
            UIViewController *initViewController = [storyBoard instantiateInitialViewController];
            [self.window setRootViewController:initViewController];
        }

//----------------HERE WE SETUP FOR IPHONE3/3s/iPod----------------------

        if(result.height == 480){
            storyBoard = [UIStoryboard storyboardWithName:@"iPhone4_Storyboard" bundle:nil];
            UIViewController *initViewController = [storyBoard instantiateInitialViewController];
            [self.window setRootViewController:initViewController];
        }
    }

        return YES;
 }

So you have created a Universal app for iPhone 3/3Gs/4/4s/5 All gen of iPod, and All type of iPad所以你已经为 iPhone 3/3Gs/4/4s/5 所有 iPod 和所有类型的 iPad 创建了一个通用应用程序

Remember to integrate all IMG with myImage.png and myImage@2x.png请记住将所有 IMG 与myImage.pngmyImage@2x.png

According to me the best way of dealing with such problems and avoiding couple of condition required for checking the the height of device, is using the relative frame for views or any UI element which you are adding to you view for example: if you are adding some UI element which you want should at the bottom of view or just above tab bar then you should take the y origin with respect to your view's height or with respect to tab bar (if present) and we have auto resizing property as well.在我看来,处理此类问题并避免检查设备高度所需的几个条件的最佳方法是使用视图的相对框架或您添加到视图中的任何 UI 元素,例如:如果您正在添加您想要的一些 UI 元素应该在视图底部或标签栏上方,那么您应该根据视图的高度或标签栏(如果存在)取 y 原点,并且我们也有自动调整大小属性。 I hope this will work for you我希望这对你有用

Rather than using a set of conditionals you can resize your view automatically using the screen size.您可以使用屏幕大小自动调整视图大小,而不是使用一组条件。

int h = [[UIScreen mainScreen] bounds].size.height;
int w = [[UIScreen mainScreen] bounds].size.width;
self.imageView.frame = CGRectMake(20, 80, (h-200), (w-100));

In my case I want a view that fills the space between some input fields at the top and some buttons at the bottom, so fixed top left corner and variable bottom right based on screen size.在我的情况下,我想要一个视图来填充顶部的一些输入字段和底部的一些按钮之间的空间,因此根据屏幕大小固定左上角和可变右下角。 My app fills the image view with the photo taken by the camera so I want all the space I can get.我的应用程序用相机拍摄的照片填充图像视图,所以我想要我可以获得的所有空间。

If you need to convert an already existing app to universal, you need to select corresponding xib file->show Utilities-> Show Size inspector.如果您需要将现有的应用程序转换为通用应用程序,则需要选择相应的 xib 文件->显示实用程序-> 显示大小检查器。

In Size inspector you can see Autosizing, by using this tool you can convert to existing iOS App.在大小检查器中,您可以看到自动调整大小,通过使用此工具,您可以转换为现有的 iOS 应用程序。

Using xCode 5, select "Migrate to Asset Catalog" on Project>General.使用 xCode 5,在 Project>General 上选择“Migrate to Asset Catalog”。

Then use "Show in finder" to find your launch image, you can dummy-edit it to be 640x1136, then drag it into the asset catalog as shown in the image below.然后使用“在查找器中显示”找到您的启动图像,您可以将其虚拟编辑为 640x1136,然后将其拖到资产目录中,如下图所示。

Make sure that both iOS7 and iOS6 R4 section has an image that is 640x1136.确保 iOS7 和 iOS6 R4 部分都有 640x1136 的图像。 Next time you launch the app, the black bars will disappear, and your app will use 4 inch screen下次启动应用程序时,黑条将消失,您的应用程序将使用 4 英寸屏幕

在此处输入图片说明

我建议根据您的 UI 界面在您的应用程序中使用 Autoresizing Mask,它可以节省很多麻烦,并且比为 iPhone 4 和 5 屏幕制作不同的 UI 更好。

值得注意的一点 - 在新的 Xcode 中,您必须将此图像文件 Default-568h@2x.png 添加到资产中

There is a slight problem when testing on both iOS device and iOS Simulator.在 iOS 设备和 iOS 模拟器上进行测试时存在一个小问题。 It appears that simulator (XCode 6.0.1) gives switched values for width and height in [[UIScreen mainScreen] bounds].size depending on a device orientation.看来模拟器(XCode 6.0.1)根据设备方向在[[UIScreen mainScreen] bounds].size给出了宽度和高度的切换值。

So this might be a problem when determinating the right physical screen size.因此,在确定正确的物理屏幕尺寸时,这可能是一个问题。 This code helps also to distinct all 2014. iPhone model generations:此代码还有助于区分所有 2014 年的 iPhone 型号:

  • iPhone4s iPhone4s
  • iPhone5 (and iPhone5s) iPhone5(和 iPhone5s)
  • iPhone6 (and iPhone6+) iPhone6(和 iPhone6+)

It can also be easily changed to make the distinction between eg iPhone6 from iPhone6+.也可以轻松更改以区分 iPhone6 和 iPhone6+。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;

    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
    {
        if (iOSDeviceScreenSize.width > 568 || // for iOS devices
            iOSDeviceScreenSize.height > 568) // for iOS simulator
        {   // iPhone 6 and iPhone 6+

            // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone6
            storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone6" bundle:nil];

            NSLog(@"loaded iPhone6 Storyboard");
        }
        else if (iOSDeviceScreenSize.width == 568 || // for iOS devices
                 iOSDeviceScreenSize.height == 568) // for iOS simulator
        {   // iPhone 5 and iPod Touch 5th generation: 4 inch screen (diagonally measured)

            // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone5
            storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone5" bundle:nil];

            NSLog(@"loaded iPhone5 Storyboard");
        }
        else
        {   // iPhone 3GS, 4, and 4S and iPod Touch 3rd and 4th generation: 3.5 inch screen (diagonally measured)

                // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
            storyboard = [UIStoryboard story    boardWithName:@"MainStoryboard_iPhone" bundle:nil];

                NSLog(@"loaded iPhone4 Storyboard");
        }
    }
    else if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
    {   // The iOS device = iPad

        storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPadnew" bundle:nil];

        NSLog(@"loaded iPad Storyboard");
    }

    // rest my code
}

Use the Auto Layout feature for views.对视图使用Auto Layout功能。 It will adjust automatically to all resolutions.它将自动调整为所有分辨率。

Create two xibs for a controller having controller name with suffix either ~iphone or ~ipad.为控制器名称后缀为 ~iphone 或 ~ipad 的控制器创建两个 xib。 At compile time, Xcode will take the right xib based on the device.在编译时,Xcode 会根据设备选择正确的 xib。

Use size classes, if you want to create a single xib for both iPhone and iPad, if the view is simple enough to port to iPhone and iPad.如果您想为 iPhone 和 iPad 创建单个 xib,如果视图足够简单,可以移植到 iPhone 和 iPad,请使用大小类。

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

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