UIScreen MainScreen Bounds returning wrong size

前端 未结 6 2024
半阙折子戏
半阙折子戏 2020-11-29 04:10

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 m

相关标签:
6条回答
  • 2020-11-29 04:55

    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 LaunchScreen file and information in info.plist file about this.

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

      UILaunchStoryboardName LaunchScreen

    The body of info.plist file if it needed:

        <?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!

    0 讨论(0)
  • 2020-11-29 04:58

    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.

    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.

    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).

    0 讨论(0)
  • 2020-11-29 05:04

    This is still dependent on the launch images as described in the answer posted in 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.

    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.

    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.

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

    0 讨论(0)
  • 2020-11-29 05:12

    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.

    This will enable the 'normal' simulator size.

    0 讨论(0)
  • 2020-11-29 05:15

    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. Use Asset Catalogs

    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

    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. 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.

    Hopefully this helps someone else who encounters this ridiculous problem.

    0 讨论(0)
  • 2020-11-29 05:15

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

    0 讨论(0)
提交回复
热议问题