设置导航栏标题的对齐方式

use*_*938 3 iphone cocoa-touch objective-c ios

我正在尝试将导航栏的标题与我的应用程序中心对齐,但似乎标题是留在右侧(请找到屏幕截图).我正在使用以下代码:

-(void)viewDidLoad {
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStylePlain target:self action:@selector(goToHomePage:)];
    [self.navigationController.navigationItem.rightBarButtonItem setTarget:self];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];
}
Run Code Online (Sandbox Code Playgroud)

viewWillAppear()下面

[self.navigationItem setTitle:offertitle];
Run Code Online (Sandbox Code Playgroud)

并试过这个

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 120, 40)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:14.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentLeft;
label.textColor =[UIColor whiteColor];
label.text=offertit;
self.navigationItem.titleView = label;
Run Code Online (Sandbox Code Playgroud)

当我隐藏后退按钮时,标题显示中心对齐的标题.是否有任何方法可以设置后退按钮的外观属性?

图像链接在这里

任何人都可以指导我可能出错的地方.

Kim*_*poy 8

您不必使用UILabel.您可以直接使用此代码:

[self.navigationItem setTitle:@"Your Title"];
Run Code Online (Sandbox Code Playgroud)

你很高兴去!此代码将自动将其自身置于导航栏的中心.

或者,如果您实际拥有UINavigationController的实例,请使用:

[self setTitle:@"Your Title"];
Run Code Online (Sandbox Code Playgroud)