Search code examples
iosobjective-cresizescreen-size

Iphone, iPad screen size with portrait and ladscape mode in programmatic way


This are getting me a little confuse. I know the 2 properties in UIScreen about the size screen, like in this question use applicationFrame, and this uses bounds.

  • applicationFrame - Show the size of device minus the statusBar. Making a NSLog in a iPhone, i get this values: (x=0, y=20, height= 460, width = 320)
  • bounds - Show all size screen. The values is: (x=0, y=0, height=480, width=320)

Now, if I rotate the device (potrait to ladscape) this will show (The bounds will be all the same value)

  • Go to function: shouldAutorotateToInterfaceOrientation: x= 0 y=20 height= 460 width= 320
  • return YES so
  • Go function: didRotateFromInterfaceOrientation: x= 20 y=0 height= 480 width= 300
  • I don't know why, but this pass one more time in shouldAutorotateToInterfaceOrientation and show: x= 20 y=0 height= 480 width= 300 (Is this a bug?)

I want to resize my view to this new size, but if I get the CGRect, it will give the correct size in the didRotateFromInterfaceOrientation and the height will be width and vice-versa.

Are there a good way to beautiful resize my view without setting this values by hand?


Solution

  • (Assuming this point was for rotating from landscape back to portrait)

    I don't know why, but this pass one more time in shouldAutorotateToInterfaceOrientation and show: x= 20 y=0 height= 480 width= 300 (Is this a bug?)

    Its not a bug!

    shouldAutorotateToInterfaceOrientation is called before the UI rotates to match the orientation of the device. So, in the first case (Portrait to Landscape) it gives you the rect for the portrait orientation. in the second case (Landscape to Portrait) it gives you the rect for the Landscape orientation.

    Bottomline:
    shouldAutorotateToInterfaceOrientation gives CGRect values before initiating the UI rotation didRotateFromInterfaceOrientation gives CGRect values after completing the UI rotation

    I suggest you look autoresizesSubviews and autoresizingMask properties of UIView. This saves a lot of headaches when you are working with views that have to rotate/resize.

    Hope I have answered your question and you find this useful!