在UINavigationBar TitleView中垂直居中标签

我没有太多的运气,我正在添加到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; 

我认为这样做的原因是我使用的实际字体有些奇怪 – 因为我不得不在button等内部进行大量的重新定位。 除了导航栏,我已经能够解决这个问题了。

打开一个古老的问题,但我发现这是非常有用的。

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

有时,根据您使用的UIFont,标题视图可能会垂直closures。

要解决这个问题,你可以使用:

 [self.navigationBar setTitleVerticalPositionAdjustment:(float) forBarMetrics:UIBarMetricsDefault]; 

(float)是一个正值,将titleView向下,负值则将titleView向上。

我希望这有帮助。

我结束了对我的问题使用了答案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]; 

如果你使用这段代码,你可能需要在标题标签的initWithFrame:call上使用“y”值。 我已经将它设置为3.0f,但是您可能需要根据自己的用法对其进行调整。

其他解决scheme似乎没有为我工作的原因是他们会水平偏离,如果我有一个barbutton(左或右)。 如果没有酒吧的button,那很好,如果有两个,那就好了。 但是,会导致它偏离中心。

我不认为这与你的字体有任何关系。 我也使用了titleView,我想它以一种奇怪的方式展示视图的方式,并没有考虑到你的视图大小。

您也可以设置标签视图的框架

 titleLabel.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); self.navigationItem.titleView = titleLabel; 
 UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName:UIFont(name:"Exo2-Bold", size: 18) as! AnyObject,NSForegroundColorAttributeName:UIColor.whiteColor()]