在UINavigationBar TitleView中垂直居中标签 [英] Centering Label Vertically in UINavigationBar TitleView

查看:73
本文介绍了在UINavigationBar TitleView中垂直居中标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm not having much luck centering vertically the label I'm adding to the TitleView on the UINavigationBar. You can see what it looks like below.

这就是我添加标签的方式:

This is how I'm adding the label:

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;

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

I think the reason why it's doing this is there is something weird with the actual font I'm using -- as I've had to do a lot of repositioning inside of buttons and such. I've been able to solve the issue everywhere but the navigation bar.

推荐答案

我最终使用了答案RPM的组合离开,我的问题之一。这是最终为我解决的问题:

I ended up using a combination of the answer RPM left and one of the comments on my question. This is what ultimately fixed it for me:

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

如果您正在使用此代码块,则可能必须使用y值在标题标签的initWithFrame:call上。我把它设置为3.0f,但你可能需要根据自己的用途进行调整。

If you're using this chunk of code, you may have to play with the "y" value on the title label's initWithFrame: call. I have it set to 3.0f, but you may have to adjust it a bit for your own usage.

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

The reason the other solutions didn't seem to work for me is they would go off-center horizontally depending if I had one barbutton (left or right). If there were no bar buttons it was fine, and if there were two it was fine. But one would cause it to go off-center.

这篇关于在UINavigationBar TitleView中垂直居中标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆