How do I calculate the optimal titleView width?
Question
I am using navigation with a custom UIView
subclass that becomes my titleView. I want to ensure this is the full available width.
Logically from my UIViewController
's viewWillAppear:
, this should be:
CGFloat width = self.width - self.navigationItem.leftBarButtonItem.width - someConstant;
(I don't have a right item here.)
This would adapt, then, for different possible widths of the leftBarButtonItem
. The catch is that leftBarButtonItem
is nil
, so leftBarButtonItem.width
is always 0 (well, in the simulator anyway).
backBarButtonItem
is also nil.
What should I be doing instead?
Solution
I hacked this by doing the following:
int vcStackSize = [[self.navigationController viewControllers] count];
WEViewController* previousController = [[self.navigationController viewControllers] objectAtIndex:vcStackSize - 2];
NSString* previousTitle = [previousController title];
UIFont* font = [UIFont boldSystemFontOfSize:12];
lw = [previousTitle sizeWithFont:font].width + 26;
Nasty but it works
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow