How to find the visible width and height in an iOS app
This article is about iOS apps and how to find the visible size (width/height) of the view.
The orientation of the device (portrait or landscape) mixes things up a little, as the system measures width and height in a fixed way, but human users perceive width and height differently when they rotate the device.
The size is usually the width and height of the window minus the height of the status bar. But your app may also contain a navigation bar, or a tab bar, or both. In any case the width usually remains the same, whereas the height may need to be calculated so that it disregards the height of status bar, navigation bar and tab bar.
The code says it better, so here is the script that does the job.
- (CGSize)get_visible_size { CGSize result; CGSize size = [[UIScreen mainScreen] bounds].size; if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) { result.width = size.height; result.height = size.width; } else { result.width = size.width; result.height = size.height; } size = [[UIApplication sharedApplication] statusBarFrame].size; result.height -= MIN(size.width, size.height); if (self.navigationController != nil) { size = self.navigationController.navigationBar.frame.size; result.height -= MIN(size.width, size.height); } if (self.tabBarController != nil) { size = self.tabBarController.tabBar.frame.size; result.height -= MIN(size.width, size.height); } return result; }
And here is an example on how to use the script above.
CGSize size = [self get_visible_size]; NSLog(@"The visible height is %f", size.height);