UIScreen MainScreen Bounds返回错误的大小 [英] UIScreen MainScreen Bounds returning wrong size

查看:580
本文介绍了UIScreen MainScreen Bounds返回错误的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我使用最新版本的XCode创建了一个新项目,并尝试记录我的应用程序的屏幕大小(以确定UI的设备类型)。我从iPhone 5运行以下代码:

So I created a new project with the latest version of XCode and tried to log the screen size of my app (to determine the device type for UI). I ran the following code from my iPhone 5:

NSLog(@"%f", [[UIScreen mainScreen] bounds].size.height);

这返回 480 ,这是屏幕旧iPhone家庭的大小。我试过模拟器,发生了同样的事情。我必须在项目中启用一些属性才能识别屏幕尺寸吗?

This returned 480, which is the screen size for the old iPhone family. I tried in the simulator and the same thing happened. Is there some property I have to enable in the project for it to recognize the screen size?

这仅适用于5个以上的设备;如果我在我的iPad上运行游戏,它会识别1024个屏幕尺寸。

This only happens for 5+ devices; if I run the game on my iPad, it recognizes the 1024 screen size.

我知道这个代码过去有用的事实。我使用完全相同的方法做了一段时间的游戏,它没有检测到屏幕大小的问题,但这是在XCode 4.x中构建的。

I know for a fact that this code has worked in the past. I made a game a while back using the exact same method and it had no problem detecting the screen size, but this was built in XCode 4.x.

我正在使用自定义视图控制器,我在App Delegate中使用以下代码创建:

I am using a custom View Controller, which I create in the App Delegate with the following code:

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

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    if([Global getDevice] == 1)
    {
        //iPhone 5+
        self.window.rootViewController = [[FivePlus alloc] initWithNibName:nil bundle:nil];

    }
    else if([Global getDevice] == 2)
    {
        //iPhone 4S-
        self.window.rootViewController = [[FourSMinus alloc] initWithNibName:nil bundle:nil];
    }
    else
    {
        //iPad
        self.window.rootViewController = [[iPad alloc] initWithNibName:nil bundle:nil];
    }

    [[self window] makeKeyAndVisible];

    // Override point for customization after application launch.
    return YES;
}



Global.h的getDevice方法:



The getDevice method from Global.h:

+ (int)getDevice
{
if([[UIScreen mainScreen] bounds].size.height == 568 || [[UIScreen mainScreen] bounds].size.width == 568)
    {
        return 1;
    }
    else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        return 3;
    }
    else
    {
        return 2;
    }
}


推荐答案

显然iOS在iPhone 5+的分辨率下依赖完全存在启动图像,以便让应用程序以该分辨率运行。

Apparently, iOS relies solely on the presence of a launch image in the resolution of an iPhone 5+ in order to let the app run in that resolution.

这个问题有两种解决方案:

There are two solutions to this problem:

1。使用资产目录

创建新项目时,会出现一个名为资产目录的东西,用于存储您的启动图像文件。将其中一个添加到您的项目和presto!

When you create a new project, there's this thing called an asset catalog which stores your launch image files. Add one of these to your project and presto!

2。挖出一些旧文件

如果你已经在XCode上呆了一段时间,你会知道在XCode 4的更新版本之一。 x,应用程序自动为您的应用程序创建三个默认启动图像文件,名为 Default.png Default@2x.png ,和 Default-568h@2x.png 。你需要在你的应用程序中使用这些文件,这些文件基本上只是黑色图像,分辨率 480x320 960x640 1136x640 ,分别注意这些是HxW,而不是WxH。

If you've been around XCode for a while, you'll know that in one of the later versions of XCode 4.x, the app automatically created three default launch image files for your app called Default.png, Default@2x.png, and Default-568h@2x.png. You need these files in your app, which are essentially just black images with the resolutions 480x320, 960x640, and 1136x640, respectively (note that these are in HxW, not WxH).


  1. 添加这些文件到支持文件组

  2. 转到项目属性并从启动图像部分选择不使用资产目录

  3. 删除资产目录。






希望这可以帮助遇到这个荒谬问题的其他人。


Hopefully this helps someone else who encounters this ridiculous problem.

这篇关于UIScreen MainScreen Bounds返回错误的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆