ios UIScreen MainScreen 边界返回错误的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21668497/
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 23:23:56  来源:igfitidea点击:

UIScreen MainScreen Bounds returning wrong size

iosiphoneobjective-cframeuiscreen

提问by David

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:

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

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

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?

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

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

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

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.

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

Additional Info:

附加信息:

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

我正在使用自定义视图控制器,我在 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;
}

The getDevice method from Global.h:

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;
    }
}

回答by David

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

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

There are two solutions to this problem:

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

1. Use Asset Catalogs

1. 使用资产目录

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. Dig out some old files

2.挖出一些旧文件

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, [email protected], and [email protected]. 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).

如果您见过世面的XCode了一段时间,你就会知道,在XCode中4.x版的后续版本中的一个,该应用程序会自动创建一个名为您的应用三个默认启动图像文件Default.png[email protected][email protected]。你需要在你的应用这些文件,基本上只是黑色图像有关决议480x320960x6401136x640,分别为(注意,这些都是高x宽,不宽x高)。

  1. Add these files to your "Supporting Files" group
  2. Go to the project properties and select "Don't Use Asset Catalogs" from the Launch Image section
  3. Delete the Asset Catalog.
  1. 将这些文件添加到您的“支持文件”组
  2. 转到项目属性并从启动图像部分选择“不使用资产目录”
  3. 删除资产目录。


Hopefully this helps someone else who encounters this ridiculous problem.

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

回答by gnasher729

iOS will often "pretend" what screen size you have. Apple assumes for example that if you don't have the right launch image for some resolution, then you haven't designed your app to work properly in that resolution, so it will run your app in a different size. In an extreme case, an iPhone only app running on an iPad will return 320 x 480.

iOS 经常会“假装”你的屏幕尺寸。例如,Apple 假设如果您没有针对某些分辨率的正确启动图像,那么您的应用程序就没有设计为在该分辨率下正常工作,因此它将以不同的大小运行您的应用程序。在极端情况下,在 iPad 上运行的仅限 iPhone 的应用程序将返回 320 x 480。

As far as your application is concerned, the screen size reported is the screen size available to your application. If it reports 320 x 480 then that is what your application can use. Anything drawn below 480 pixels will not be visible.

就您的应用程序而言,报告的屏幕尺寸是您的应用程序可用的屏幕尺寸。如果它报告 320 x 480,那么这就是您的应用程序可以使用的。低于 480 像素绘制的任何内容都将不可见。

You convince iOS to run your app in the resolution that you want for example by supplying a launch image in the right size. In the case of iPhone 6 and 6+, the user can run them in "Zoom" mode so they behave as if they had the screen of an iPhone 5 or 6 (just physically bigger).

您说服 iOS 以您想要的分辨率运行您的应用程序,例如通过提供正确大小的启动图像。在 iPhone 6 和 6+ 的情况下,用户可以在“缩放”模式下运行它们,这样它们的行为就好像拥有 iPhone 5 或 6 的屏幕(只是物理上更大)。

回答by Jasper

In XCode6 having no splash screen image for iPhone5 screen size will trigger a warning. XCode will add a default one (just plain black) if you press the warning.

在 XCode6 中没有 iPhone5 屏幕尺寸的启动画面图像将触发警告。如果您按下警告,XCode 将添加一个默认值(纯黑色)。

This will enable the 'normal' simulator size.

这将启用“正常”模拟器大小。

回答by Nicolas Manzini

In iOS 8 [[UIScreen mainScreen] bounds].size.height);return something inverted wether the device is in vertical or landscape mode.

在 iOS 8 [[UIScreen mainScreen] bounds].size.height);中,无论设备处于垂直模式还是横向模式,都会返回倒置的内容。

回答by Alex Kolovatov

I faced this problem when opening an old project. So, I found the solution. In my case, the problem is that I don't have LaunchScreenfile and information in info.plistfile about this.

我在打开一个旧项目时遇到了这个问题。所以,我找到了解决方案。就我而言,问题是我没有关于此的LaunchScreen文件和信息info.plist

  1. Right-lick on your info.plist -> Open As -> Source Code
  2. Add this two lines between <dict>and </dict>

    UILaunchStoryboardName LaunchScreen

  1. 右键单击您的 info.plist -> 打开为 -> 源代码
  2. <dict>和之间添加这两行</dict>

    UILaunchStoryboardName LaunchScreen

The body of info.plist file if it needed:

info.plist 文件的正文(如果需要):

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>armv7</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
</dict>
</plist>

I hope, It will help!

我希望,它会有所帮助!

回答by aeu

This is still dependent on the launch images as described in the answer posted in 2014.

这仍然取决于 2014 年发布的答案中所述的启动图像。

Since that answer was posted Apple has released a lot of products with different screen resolutions, so solution #2 in that answer no longer works for all devices.

自从发布该答案以来,Apple 发布了许多具有不同屏幕分辨率的产品,因此该答案中的解决方案 #2 不再适用于所有设备。

As of XCode 10.3, the way to do this is to create a launch image asset catalog in your project and then and populate it with images. Specifically, 27 different launch images of varying sizes for all possible options.

从 XCode 10.3 开始,这样做的方法是在您的项目中创建一个启动图像资产目录,然后用图像填充它。具体来说,所有可能的选项都有 27 个不同大小的不同启动图像。

I made the full set for a project I was working on, these are posted on github so that you can save yourself the fun I had making all these images.

我为我正在做的一个项目制作了全套,这些都张贴在 github 上,这样你就可以省去我制作所有这些图片的乐趣。

https://github.com/aeu/ios-launch-images

https://github.com/aeu/ios-launch-images