我在将要添加到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;

我认为执行此操作的原因是我使用的实际字体有些奇怪-因为我不得不在按钮等内部进行很多重新定位。除了导航栏外,我已经能够解决所有问题。

最佳答案

打开一个旧的Question,但是我发现这非常有用。

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/

10-12 04:26