ios - UIScreen MainScreen Bounds 返回错误的大小

标签 ios iphone objective-c frame uiscreen

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

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

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

这只发生在 5 台以上的设备上;如果我在 iPad 上运行游戏,它会识别 1024 的屏幕尺寸。

我知道这段代码过去一直有效。不久前我使用完全相同的方法制作了一个游戏,它检测屏幕大小没有问题,但这是在 XCode 4.x 中构建的。

附加信息:

我正在使用自定义 View Controller ,我使用以下代码在 App Delegate 中创建它:

- (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 方法:

+ (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+ 分辨率的启动图像的存在,以便让应用程序以该分辨率运行。

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

1.使用 Assets 目录

当您创建一个新项目时,有一个称为 Assets 目录的东西,它存储您的启动图像文件。将其中之一添加到您的项目中即可!

<强>2。挖出一些旧文件

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

  1. 将这些文件添加到您的“支持文件”组中
  2. 转到项目属性并从“启动图像”部分选择“不使用 Assets 目录”
  3. 删除 Assets 目录。

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

关于ios - UIScreen MainScreen Bounds 返回错误的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21668497/

相关文章:

ios - 我可以在 UITableViewController 编辑模式下删除一些而不是全部单元格吗

ios - iCarousel 配置任务

ios - UICollectionView 重新加载数据但不显示新单元格

iphone - 数据排序

iphone - NSXMLParser 遇到特殊字符后停止解析

iphone - UITableView reloadData 方法造成数据模糊的麻烦

ios - 如何在 iOS 中为 SonarQube Analyzer 包含 Pod 或 Frameworks 等依赖项目

iPhone UINavigationBar 向上/向下移动

iOS Photokit - PHAsset pixelWidth 和 pixelHeight 与高分辨率图像不匹配

javascript - 读磁墨水(MICR)