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
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 in viewWillLayoutSubviews
.
You should use self.view.bounds
instead.
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,
NSLog("Frame: %@", NSStringFromCGRect(self.view.frame));
NSLog("Frame: %@", NSStringFromCGRect(self.view.window.frame));
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,
CGRect frame = [[UIScreen mainScreen] applicationFrame];
CGSize size = frame.size;
if (![UIUtil isAfterIOS8]){
size.width = frame.size.height;
size.height = frame.size.width;
}
/* UIUtil.m */
+ (BOOL) isAfterIOS8 {
return [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0;
}
You can log it as :
NSLog(@"My view's frame is: %@", NSStringFromCGRect(myView.frame));
Or try this
CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
Or
CGFloat height = [self.view frame].size.height;
CGFloat width = [self.view frame].size.width;
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.