Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I vertically correct the navigationBar's titleView text position when using a custom font?

We're using custom fonts for the titleView in the navigation bar. Somehow Apple always draws this font too high.

How do I correct for this strange offset you get when you are using custom fonts in a navbar?

like image 753
Tycho Pandelaar Avatar asked Dec 12 '11 13:12

Tycho Pandelaar


2 Answers

Your can set a new view as titleView, then add a new label to it:

UIView * customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 40.0f)];

UILabel * customLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 200.0f, 20.0f)];
[customLabel setBackgroundColor:[UIColor clearColor]];
[customLabel setTextColor:[UIColor whiteColor]];
[customLabel setFont:[UIFont systemFontOfSize:12.0f]];
[customLabel setTextAlignment:UITextAlignmentCenter];
[customLabel setText:@"Your Text"];
[customTitleView addSubview:customLabel];
[customLabel release];

[self.navigationItem setTitleView:customTitleView];
[customTitleView release];
like image 40
Kjuly Avatar answered Nov 07 '22 19:11

Kjuly


I used setTitleVerticalPositionAdjustment:forBarMetrics:.

Compatibility: available starting from iOS 5.

like image 77
Vadoff Avatar answered Nov 07 '22 17:11

Vadoff