1

on any other device we use this code to set all views in landscape and everything fits great :

float width=[UIScreen mainScreen].bounds.size.width;
float height= [UIScreen mainScreen].bounds.size.height;

on the iPad 2 only , in order to make it work on landscape we have to swap the width and height, otherwise he puts views on portrait and they seems ugly.

Why is it happens only in iPad2 ?

Curnelious
  • 1
  • 16
  • 76
  • 150
  • What's the iOS version of the devices? http://stackoverflow.com/questions/24150359/is-uiscreen-mainscreen-bounds-size-becoming-orientation-dependent-in-ios8 ? – Larme Jun 09 '15 at 16:14
  • ios7. i can see now in 7 you dont get the real screen size! how can i fix it ? do i have to go over all my code now ? – Curnelious Jun 09 '15 at 16:15
  • It's a well know problem that you would have found with some search. Check the screen orientation. And read up on iOS 9 soon, where getting the screen bounds i very likely _not_ a good idea anymore. – gnasher729 Jun 09 '15 at 16:53
  • thanks but what happens still is that when you switch views, you see for a second the portrait and not the landscape . why is that ? – Curnelious Jun 09 '15 at 16:55

3 Answers3

3

It's not linked to the device but to iOS. Since iOS 8.0, the bounds is now dependent of the device orientation. I'm also swapping width and height like this :

CGRect ScreenBounds() { CGRect bounds = [UIScreen mainScreen].bounds; CGRect tmp_bounds = bounds; if(bounds.size.width < bounds.size.height) { bounds.size.width = tmp_bounds.size.height; bounds.size.height = tmp_bounds.size.width; } return bounds; }

tYp
  • 121
  • 9
  • I have put this in my code but he will never call this function – Curnelious Jun 09 '15 at 16:27
  • Just like this : CGRect bounds = ScreenBounds(); Then use bounds.width and bounds.height like you would do with the code you provide – tYp Jun 09 '15 at 16:42
  • I put this function in class extension of NSObject, like this I can call it everywhere in the code – tYp Jun 09 '15 at 16:47
  • thanks but what happens still is that when you switch views, you see for a second the portrait and not the landscape . why is that ? – Curnelious Jun 09 '15 at 16:55
  • What do you mean by switching views ? Have you some code to provide maybe ? :) – tYp Jun 09 '15 at 17:30
2

Don't size your views relative to the screen, but relative to their container view. Simple as that.

Christian Schnorr
  • 10,768
  • 8
  • 48
  • 83
1

I had same issue From iOS 8 UIScreen is interface oriented so you will get proper results on devices which are running on iOS 8.

In order to support iOS 7 as well you can use following util method:

+ (CGSize)screenSize {
CGSize screenSize = [UIScreen mainScreen].bounds.size;
if ((NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
    return CGSizeMake(screenSize.height, screenSize.width);
}
return screenSize;
}
Amit Parpiyani
  • 168
  • 1
  • 1
  • 8
  • but how? how can i use it ? is it override the original ?? – Curnelious Jun 09 '15 at 16:32
  • You can add this class method to util class or any of your class and getsize and then use suze struct for width and height. For example :CGSize sizeRect = [Utility screenSize]; float width = sizeRect.width; float height = sizeRect.height; – Amit Parpiyani Jun 09 '15 at 16:39
  • cant you override the UIScreen ? – Curnelious Jun 09 '15 at 16:48
  • 1
    This is you custom class you can add this method to any of you class even on your view controller where you are using it. But best practice says you should add these kind of helper methods to one helper class that will be used by all the classes. Yo can create a category as well. – Amit Parpiyani Jun 09 '15 at 16:57