我在 UINavigationBar
上将要添加到 TitleView 的标签垂直居中的运气不太好。您可以在下面看到它的样子。
这就是我添加标签的方式:
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = NSLocalizedString(@"activeSessionsTitle",@"");
titleLabel.font = [Util SETTING_NEO_HEADER_FONT];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f];
titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f);
[titleLabel sizeToFit];
super.navigationItem.titleView = titleLabel;
我认为它这样做的原因是我使用的实际字体有些奇怪——因为我不得不在按钮等内部进行大量重新定位。除了导航栏,我已经能够解决所有地方的问题。
最佳答案
打开一个旧问题,但我发现这非常有用。
iOS 5 允许您使用[UINavigationBar appearance]
,这非常适合更改标题栏的标题 View ,而无需自定义 UIView:
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor],
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"HelveticaNeue" size:25.0f],
UITextAttributeFont,
nil]];
有时,根据您使用的 UIFont,标题 View 可能会垂直偏移。
要解决这个问题,您可以使用:
[self.navigationBar setTitleVerticalPositionAdjustment:(float) forBarMetrics:UIBarMetricsDefault];
(float)
为正值将 titleView 向下推,为负值将 titleView 向上推。
希望对您有所帮助。
关于ios - 在 UINavigationBar TitleView 中垂直居中标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9915636/