在UINavigationBar TitleView中垂直居中标签

Hos*_*yer 7 uinavigationbar uilabel ios

我没有太多的运气垂直居中我正在添加到TitleView上的标签UINavigationBar.你可以在下面看到它的样子.

文字未垂直对齐

这是我添加标签的方式:

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;
Run Code Online (Sandbox Code Playgroud)

我认为它之所以这样做是因为我使用的实际字体有些奇怪 - 因为我必须在按钮内部进行大量重新定位等等.除了导航栏,我已经能够解决所有问题.

Moe*_*Moe 28

打开一个旧问题,但我发现这非常有用.

iOS 5允许您使用[UINavigationBar appearance],这对于更改标题栏的标题视图非常有用,而无需自定义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]];
Run Code Online (Sandbox Code Playgroud)

有时,根据您使用的UIFont,标题视图可能会垂直关闭.

要解决此问题,您可以使用:

[self.navigationBar setTitleVerticalPositionAdjustment:(float) forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

(float) 是推动titleView向下的正值,以及推动titleView向上的负值.

我希望这有帮助.

  • 您还可以使用UINavigationBar外观设置垂直位置调整,即:`[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:(float)forBarMetrics:UIBarMetricsDefault];` (7认同)
  • 你刚刚救了我的脖子,先生!采取我的upvote!:) (2认同)

Hos*_*yer 11

我最后使用了左边的答案RPM和我的问题上的一条评论的组合.这是最终为我解决的问题:

UIView *customTitleView = [[UIView alloc] init];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 3.0f, 200.0f, 30.0f)];
titleLabel.text = titleString;
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];

customTitleView.frame = CGRectMake(self.navigationItem.titleView.frame.size.width/2 - titleLabel.frame.size.width/2, self.navigationItem.titleView.frame.size.height/2 - titleLabel.frame.size.height/2, titleLabel.frame.size.width, titleLabel.frame.size.height);

[customTitleView addSubview:titleLabel];
[titleLabel release];

[self.navigationItem setTitleView:customTitleView];
[customTitleView release];
Run Code Online (Sandbox Code Playgroud)

如果你正在使用这段代码,你可能必须在标题标签的initWithFrame:call上使用"y"值.我将它设置为3.0f,但您可能需要根据自己的使用情况进行调整.

其他解决方案似乎对我不起作用的原因是,如果我有一个按钮(左或右),它们会偏离水平偏心.如果没有条形按钮就可以了,如果有两个就可以了.但是会让它偏离中心.