如何在使用自定义字体时垂直更正navigationBar的titleView文本位置?

我们在导航栏中使用titleView的自定义字体。 不知何故,苹果总是把这种字体画得太高。

在导航栏中使用自定义字体时,如何纠正这个奇怪的偏移量?

我用setTitleVerticalPositionAdjustment:forBarMetrics:

兼容性:从iOS 5开始。

你可以设置一个新的视图为titleView ,然后添加一个新的标签:

 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];