-1

我无法设置以编程方式分配UILabel以在我的 NavigationItem 的 titleView 中居中。我什至将 NSAlignment 设置为 NSAlignmentCenter。这是它的样子:

如您所见..我没有使用正确的 UIBarButtonItem ,但这不应该使文本保持居中吗?这是代码,在viewDidLoad

//Set the title of navBar----------

UILabel  *navTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];  //0 0 320 10

navTitleLabel.backgroundColor = [UIColor clearColor];
navTitleLabel.textAlignment = NSTextAlignmentCenter;
navTitleLabel.textColor=[UIColor whiteColor];
navTitleLabel.text = @"IMG";
[navTitleLabel setFont: [UIFont fontWithName:@"BullettoKilla" size:16]];

self.navigationItem.titleView = navTitleLabel;

我究竟做错了什么?

4

2 回答 2

0

正如0yeoj所指出的,您已将项目宽度设置为 320。尝试更小的东西。

于 2015-08-20T04:42:47.263 回答
-1

尝试这个 UILabel *navTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 40)];

于 2015-08-20T04:51:05.090 回答