Search code examples
iosuinavigationitem

Get Navigation Title Width always return 0 value?


In the ViewWillApear I do the following code: (theTitle has a string value)

self.navigationItem.title =  theTitle;

DLog(@"Title Width : %f", CGRectGetWidth(self.navigationItem.titleView.frame));

I always get Title Width : 0.000000. I also tried the following code and I also have 0 value

CGRect titleViewFrame = [self.navigationItem.titleView convertRect:self.navigationItem.titleView.bounds toView:self.view];
DLog(@"%f", titleViewFrame.size.width);

Thnaks for you help


Solution

  • When presented with a UINavigationController the navigationItem object of a view controller is nil.

    You can get the frame you want by using the navigationItem of the navigationController:

    [[[self.navigationController navigationItem] titleView] frame]