Inside the viewDidLoad
method of a UIViewController:
NSLog(@"frame: %f %f", self.view.frame.size.width, self.view.frame.size.height);
With the device in landscape:
frame: 748.000000 1024.000000
With the device in portrait:
frame: 768.000000 1004.000000
You should use
self.view.bounds
instead.That is the current size of the main view in your view controller. The views are measured in points.
Note that the size of the iPad screen in points is 1024x768, but the size you are getting in the NSLog is without the status bar on top, which is 20 points tall.
In
viewDidLoad
, the frame may not have adjusted to all of the things that affect its final size, such as rotation, other views, etc. It's generally best to only create and set up your objects here; you can inspect the finalized frame inviewWillLayoutSubviews
.You can log it as :
Or try this
Or
There is no size of the a view controller. Its the size of the view or the window. you can use the
.frame
or.bounds
method to get it. Also you can easily print the rect using,You can also use the
[UIScreen mainScreen].bounds
to get the size of the full screen. You will always get that 20 pixel offset in x or y direction, for the status bar, depending on your orientation.Whatever happens, the frame returns the height and width, assuming the device is in portrait mode, with the home button down. So when the device is in landscape mode, the status bar is on the right side, and the width is 20 pixel less to make room for the status bar.
Edited: After iOS 8 Now you get the width and height correctly in portrait and landscape orientation. i.e. as it looks -- in landscape width=portrait height. When I use in my code,