Programmatically determine the maximum usable frame size for a UIView
Solution 1
I think you're putting your method in the wrong place. UIScreen knows about the screen, but it doesn't (and shouldn't) know anything about what's displayed on the screen. It's the view controller that's responsible for managing the view, so that's where the method belongs. Furthermore, since the max frame depends on the configuration of a particular view controller, this should be an instance method and not a class method. I think you should add a category to UIViewController with the method:
- (CGRect) maximumUsableFrame;
It's still fairly generic, available to all view controllers, but at the same time has access to view controller properties like navigationController
and tabBarController
.
Solution 2
Use applicationFrame. [[UIScreen mainScreen] applicationFrame]
This is how I use it to crop stuffs in my screenshot.
Here is some sample code.
-(UIImage*) crop20PointsifStatusBarShowsUp
{
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; //Look at this
float sizeOfStatusBarVar= applicationFrame.origin.y;
if ([BGMDApplicationsPointers statusBarShowUp])
{
CGRect newSize = CGRectMake(0, sizeOfStatusBarVar, self.size.width, self.size.height-sizeOfStatusBarVar);
UIImage * newImage = [self cropUIImageWithCGRect:newSize];
return newImage;
}
else{
return [self copy];
}
}
Magic Bullet Dave
Self taught programmer (10 year old with a ZX-81 then a Spectrum). Did an Engineering degree and only really enjoyed the computing lectures. Spent 10 years at a software company consulting on Visual Basic. Left in 2002 to start my own media production company. Realised I needed to feed my inner geek and do some coding again. Was about to buy a Spectrum off ebay and have a go at some really low level stuff... until I got an iPhone... 3 Years down the track and loving Objective C, Cocoa-Touch and the Apple product set.
Updated on June 06, 2022Comments
-
Magic Bullet Dave almost 2 years
I have seen several similar questions to this, but none that addresses my specific need. I want to be able to write a generic helper method that returns the maximum usable frame size for a UIView, taking into account whether the app has any combination of a status bar, navigation bar and/or tab bar as I find myself doing this all the time.
Method definition would be as an extension of UIScreen:
+ (CGRect) maximumUsableFrame;
Getting the size with or without the status bar can be got from the
[UIScreen mainScreen].applicationFrame
property, but I cannot figure out a way of determining if there is a navigation bar or tab bar present. I've thought about maintaining some global flags in my app delegate but this seems really clunky and stops the code being generic and re-usable. I have also considered passing a UIView as a parameter, getting the view's window, then the rootViewController and then seeing if the navigation controller property is set. If so then checking if the navigation controller is hidden. All very clunky if you ask me.
Any thoughts would be appreciated.
Dave
EDIT: Incorporating ideas from Caleb's answer in case this is of use to anyone else:
// Extension to UIViewController to return the maxiumum usable frame size for a view @implementation UIViewController (SCLibrary) - (CGRect) maximumUsableFrame { static CGFloat const kNavigationBarPortraitHeight = 44; static CGFloat const kNavigationBarLandscapeHeight = 34; static CGFloat const kToolBarHeight = 49; // Start with the screen size minus the status bar if present CGRect maxFrame = [UIScreen mainScreen].applicationFrame; // If the orientation is landscape left or landscape right then swap the width and height if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) { CGFloat temp = maxFrame.size.height; maxFrame.size.height = maxFrame.size.width; maxFrame.size.width = temp; } // Take into account if there is a navigation bar present and visible (note that if the NavigationBar may // not be visible at this stage in the view controller's lifecycle. If the NavigationBar is shown/hidden // in the loadView then this provides an accurate result. If the NavigationBar is shown/hidden using the // navigationController:willShowViewController: delegate method then this will not be accurate until the // viewDidAppear method is called. if (self.navigationController) { if (self.navigationController.navigationBarHidden == NO) { // Depending upon the orientation reduce the height accordingly if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) { maxFrame.size.height -= kNavigationBarLandscapeHeight; } else { maxFrame.size.height -= kNavigationBarPortraitHeight; } } } // Take into account if there is a toolbar present and visible if (self.tabBarController) { if (!self.tabBarController.view.hidden) maxFrame.size.height -= kToolBarHeight; } return maxFrame; }
-
Alexander over 11 yearsIf NavigationBar and TabBar are visible, you can find heights this way:
self.navigationController.navigationBar.frame.size.height;
andself.tabBarController.tabBar.frame.size.height
. Why variable for tabbar height called kToolBarHeight? =) -
Bill Burgess over 10 yearsDepending how you want to use this, at least in my case, I also updated the frame origin for the navController so I knew exactly where I could position my view in the ViewController. Otherwise you could still end up under the nav bar.
-
-
Magic Bullet Dave over 12 yearsThanks for the pointers all good stuff and makes a lot of sense. Will write the extension and let you know how I get on.
-
Magic Bullet Dave over 12 yearsHave added a category to the UIViewController and edited my original question to include the code and would appreciate your thoughts. Cheers Dave.
-
Isak almost 11 yearsNo idea why this isn't the top answer.
-
Austin Haws about 10 yearshad to comment the maxBounds.origin.y = statusBarOffset; and maxBounds.origin.y += self.navigationController.navigationBar.frame.size.height; in a different app this was used in.