UINavigationItem titleView位置

ind*_*gie 15 iphone cocoa-touch objective-c uinavigationitem ipad

我正在使用UINavigationItem的titleView属性来设置我想要的字体大小/颜色的自定义UILabel.这是我的代码:

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

在导航栏中,我还有一个左栏按钮.结果是这样的:

alt text http://cl.ly/41164eec656c96e7c913/content

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

Sjo*_*ost 19

在代替的initWithFrame只是使用init和投入[self.headerLabel sizeToFit] 的代码的最后一行.

  • 这就像一个魅力.没有人应该投票支持这一点. (2认同)

Wil*_*sch 15

如果你使headerLabel成为titleView的子视图,你可以设置headerLabel的框架来控制它在titleView中的位置.

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

希望这可以帮助!