I have a navigationBar with both Left and Right bar buttons on each side. I have a customTitlelabel
which I set as the titleView of the UINavigationItem
I had a similar situation where a titleView
should be centered in UINavigationBar. I like occulus's approach of subclassing a UIView and overriding setFrame:
. Then, I can center the frame inside the dimensions of UINavigationBar.
In the UIView subclass:
-(void)setFrame:(CGRect)frame{
super.frame = CGRectMake(320 / 2 - 50, 44 / 2 - 15, 100, 30);
}
The UIView subclass can then be assigned normally to titleView
for each navigationItem. The developer does not have to programmatically add and remove special subviews from UINavigationBar.