Note that there are some explanatory texts on larger screens.

plurals
  1. POProgrammatically determine the maximum usable frame size for a UIView
    primarykey
    data
    text
    <p>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.</p> <p>Method definition would be as an extension of UIScreen:</p> <pre><code>+ (CGRect) maximumUsableFrame; </code></pre> <p>Getting the size with or without the status bar can be got from the</p> <p><code>[UIScreen mainScreen].applicationFrame</code> </p> <p>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.</p> <p>Any thoughts would be appreciated.</p> <p>Dave</p> <p>EDIT: Incorporating ideas from Caleb's answer in case this is of use to anyone else:</p> <pre><code>// 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload