UINavigationItem titleView position [英] UINavigationItem titleView position

查看:188
本文介绍了UINavigationItem titleView position的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用UINavigationItem的titleView属性设置一个自定义的UILabel与我想要的字体大小/颜色。这是我的代码:

I'm using UINavigationItem's titleView property to set a custom UILabel with my desired font size/color. Here's my code:

self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 400.0, 44.0)];
self.headerLabel.textAlignment = UITextAlignmentCenter;
self.headerLabel.backgroundColor = [UIColor clearColor];
self.headerLabel.font = [UIFont boldSystemFontOfSize:20.0];
self.headerLabel.textColor = [UIColor colorWithRed:0.259 green:0.280 blue:0.312 alpha:1.0];
self.navigationItem.titleView = self.headerLabel;

在导航栏中,我还有一个左侧栏按钮。其结果是:

In the navigation bar I also have a left bar button. The result is this:

如您所见,文本未正确居中。我已经尝试设置标签的x原点,但这没有效果。

As you can see, the text isn't properly centered. I've tried setting the x origin of the label, but this has no effect.

推荐答案

如果你使headerLabel是一个子视图的titleView,你可以设置headerLabel的框架控制它在titleView 。

If you make the headerLabel a subview of the titleView, you can then set headerLabel's frame to control where it goes within the titleView.

现在的方式,你没有这个控制。我认为操作系统会根据可用的空间为您选择titleView的框架。

The way you are doing it now, you don't have that control. I think the OS chooses the titleView's frame for you based on the space available.

希望这有助于!

这篇关于UINavigationItem titleView position的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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