我正在使用 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;
在导航栏中我还有一个左栏按钮。结果是:文本没有正确居中。我试过设置标签的 x 原点,但这没有效果。
最佳答案
代替 initWithFrame
只需使用 init
并将 [self.headerLabel sizeToFit]
放在你的最后一行代码。
关于iphone - UINavigationItem title查看位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3125245/