Search code examples
objective-ciosxcodeviewdimensions

Why is self.view height and width different than a sub view's height and width?


What is the logic behind the fact the main screen and self.view always maintain the same width and height dimensions regardless of orientation (even as the self.view does re-size correctly to fit the current device orientation), while a sub-View of self.view returns modified width and height dimensions when the device orientation changes?

NSLog(@"screen bounds w: %f, h: %f", [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height );

NSLog(@"self.view frame w: %f, h: %f", self.view.frame.size.width, self.view.frame.size.height );

NSLog(@"myView frame w: %f, h: %f", _myView.frame.size.width, _myView.frame.size.height );

Log output portrait

2012-11-05 16:15:11.152 test[2807:11303] screen bounds w: 320.000000, h: 480.000000
2012-11-05 16:15:11.153 test[2807:11303] self.view frame w: 320.000000, h: 460.000000
2012-11-05 16:15:11.153 test[2807:11303] myView frame w: 320.000000, h: 460.000000

Log output landscape

2012-11-05 16:14:38.800 test[2807:11303] screen bounds w: 320.000000, h: 480.000000
2012-11-05 16:14:38.801 test[2807:11303] self.view frame w: 300.000000, h: 480.000000
2012-11-05 16:14:38.801 test[2807:11303] myView frame w: 480.000000, h: 300.000000

Solution

  • Actually it more than auto-resizing masks. So when you change the orientation of the device, the physical characteristics of the screen remains the same. If you set your view to be auto rotate, the width and height are translated for you. So when asking for screen dimensions with translated views, its imp. for you to do calculations based on the self.view.center or self.view.bounds rather than on self.view.frame.