Get CGRect frame of inner contentView of UINavigationController during or before loadView

Apple docs say:

Note: Because the amount of space available for the custom view can vary
(depending on the size of the other navigation views), your custom view’s
autoresizingMask property should be set to have a flexible width and height.
Before displaying your view, the navigation controller automatically
positions and sizes it to fit the available space.


This is great, except ... I need to know the size that the UINavController will use BEFORE adding the view to the frame, because the content that I will be displaying differs depending on the size.

This is inside the loadView method for the UIViewController, which is added to the UINavigationController.


source to share

3 answers

Well, technically you shouldn't be picking up your subzones in the method loadView

. I do not use nibs, and in almost all control of my presentation loadView

is really little: self.view = [[UIView alloc] init];

. Then in viewDidLoad

I add all my routines to self.view

. Finally, I make all the changes to viewWillAppear

, because at that point the self.view

size will be appropriate.

If you have to evaluate views in a method loadView

, you will need to calculate the dimensions manually ... i.e .: find out the device orientation ( self.interfaceOrientation

), determine the screen size, subtract all other elements that you think are on the screen (for example, a panel navigation, etc.). But this method is not recommended.



Since the view hierarchy inside the navigation controller is not documented, there is probably no reliable way to get this information.

You'll have to:

  • Or inquire self.navigationController.view

    for your boundaries and manually subtract the room occupied by the navbar (if navigationBarHidden

    is NO

    ) and the toolbar (if toolbarHidden

    - NO


  • Or manually navigate to subzones self.navigationController.view

    and try to identify the content view (if such a thing exists) or the space occupied by other views.

Both solutions rely on internals that may change in future iOS releases and are therefore far from optimal. Can't you wait until viewDidLoad

deciding which content to display?



How about using KVO for the viewController.view.frame property?



All Articles