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

P5y*_*cH0 10 iphone uinavigationbar uilabel uinavigationitem ios

我们在导航栏中为titleView使用自定义字体.不知何故Apple总是把这个字体画得太高了.

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

Vad*_*off 23

我用过setTitleVerticalPositionAdjustment:forBarMetrics:.

兼容性:从iOS 5开始提供.

  • 太棒了,值得继续阅读!没有SO,生活会怎样?[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:2.f forBarMetrics:UIBarMetricsDefault]; (4认同)

Kju*_*uly 13

您可以将新视图设置为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];
Run Code Online (Sandbox Code Playgroud)

  • 这是我一直在考虑的一种解决方法.太糟糕的自定义字体以这种方式行事...... (2认同)