Uiscreen Mainscreen Bounds Returning Wrong Size

UIScreen MainScreen Bounds returning wrong size

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.

UIScreen mainScreen bounds give wrong size

you can do the following.

CGRect rect = [UIScreen mainScreen].bounds;

CGRect screenFrame = CGRectMake(0, [[UIApplication sharedApplication] statusBarFrame].size.height, rect.size.width, rect.size.height - [[UIApplication sharedApplication] statusBarFrame].size.height);

Now you can use this screenFrame, I hope this will resolve your problem.

Is [UIScreen mainScreen].bounds.size becoming orientation-dependent in iOS8?

Yes, it's orientation-dependent in iOS8, not a bug. You could review session 214 from WWDC 2014 for more info: "View Controller Advancements in iOS 8"

Quote from the presentation:

UIScreen is now interface oriented:

  • [UIScreen bounds] now interface-oriented
  • [UIScreen applicationFrame] now interface-oriented
  • Status bar frame notifications are interface-oriented
  • Keyboard frame notifications are interface-oriented

return value of [UIScreen mainScreen].bounds.size.height on iphone 6

Add the splash image for iPhone 6, then it will work as you want

Click on images.xcassets, click on attribute inspector and choose iOS 8.0 and Later

choose iOS 8.0 and Later

then you will get option for two extra launch image like below
Drag drop the retina size image for iPhone 6 and iPhone 6 plus in Retina HD 4.7 and Retina HD 5.5 respectively

Add launch image for Retina HD

iPad wrong [[UIScreen mainScreen] bounds].size dimensions

The screen size will always be the same regardless of device orientation. The same with the key window size. The view controllers are the ones that rotate inside the window. So you either rotate your view if you're adding it to the window directly or simply add it to the root view controller, which will have a correct size.

For example:

UIWindow* keyWindow = [UIApplication sharedApplication].keyWindow;
UIViewController* rootController = keyWindow.rootViewController;

CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGSize windowSize = keyWindow.bounds.size;
CGSize rootControllerSize = rootController.view.frame.size;
CGSize rootControllerBoundsSize = rootController.view.bounds.size;
CGAffineTransform rootTransform = rootController.view.transform;

NSLog(@"Screen size: %@", NSStringFromCGSize(screenSize));
NSLog(@"Window size: %@", NSStringFromCGSize(windowSize));
NSLog(@"RootController frame size: %@", NSStringFromCGSize(rootControllerSize));
NSLog(@"RootController bounds size: %@", NSStringFromCGSize(rootControllerBoundsSize));
NSLog(@"RootController transform: %@", NSStringFromCGAffineTransform(rootTransform));

if executed in landscape will print the following:

Screen size: {768, 1024}
Window size: {768, 1024}
RootController frame size: {768, 1024}
RootController bounds size: {1024, 768}
RootController transform: [0, 1, -1, 0, 0, 0]

The key here is the transform that it's applied to the root UIViewController when the device is rotated.

To obtain the screen size taking into account the device rotation you can use the root controller bounds size.



Related Topics



Leave a reply



Submit