iphone - UINavigationItem title查看位置

标签 iphone objective-c cocoa-touch ipad uinavigationitem

我正在使用 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/

相关文章:

iphone - 如何从带有标签和颜色对象的方法中选择随机颜色?

objective-c - 在 Quick Look PreviewController 的顶部显示模态视图 Controller 导致棋盘屏幕?

ios - iOS 中的 NSString 到 NSData

ios - 来自 plist 的分段 UITableview

cocoa - 不使用现有 NSCoding 方法来实现 NSCopying 的任何理由

iphone - 使用 FBConnect 通过 Facebook 通过 iPhone 应用程序对用户进行身份验证的 Rails 后端服务器进行身份验证

iphone - 检查 iPhone 中的网络可达性后应用程序崩溃?

iphone - 覆盖 layoutSubviews 调用 [super layoutSubviews] 然后找到并重新定位按钮的 View

iphone - NSKeyedArchiver 的对象顺序

ios - 是否可以将 `marshal` 数据放入 Realm 对象中?