How to determine the size / frame of a custom UINavigationItem.titleView?

After you create a custom view and assign its navigationItem.titleView property, it appears as follows

enter image description here

with a custom view filling the gap between the two buttons. Therefore, the user view is not oriented to the navigation bar. How to define a view frame in a .titleView property? I want to focus the text on the navigation bar, say, under the time stamp.

+5
source share
2 answers

titleView ( ), :

[self.navBar layoutIfNeeded];
CGRect titleViewFrameInTopLevelViewSpace = [self.navigationItem.titleView
    convertRect:self.navigationItem.titleView.bounds
    toView:self.view];

layoutIfNeeded, titleView, , .

titleView , . , ( ) . :

  • titleView XIB. :
    XIB screen shot
    128x33. . , "". .

  • titleView viewDidLoad:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 33)];
        customView.backgroundColor = [UIColor greenColor];
        self.navItem.titleView = customView;
     }
    

    :
    Simulator screen shot

+14

leftBarButtonItem rightBarButtonItem , , , , titleView. , ?

+2

All Articles