设置导航栏标题的对齐方式 [英] set the alignment of title of navigation bar

查看:979
本文介绍了设置导航栏标题的对齐方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将导航栏的标题对准我应用程序的中心,但似乎标题停留在右侧(请找到屏幕截图)。我使用下面的代码:

I'm trying to align the title of the navigation bar to the center in my app but it seems the title is staying on the right side (please find the screen shot). I'm using the code below:

-(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];
}

in viewWillAppear()如下

[self.navigationItem setTitle:offertitle];

并尝试过。

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;

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

the title displays center aligned title when I hide the back button. Is there any method to set the appearance attributes of back button?

图片链接到这里

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

Can anyone guide me where i may have gone wrong.

推荐答案

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

You don't have to use UILabel. You can directly use this code:

[self.navigationItem setTitle:@"Your Title"];

您很好!

或者如果你实际上有一个UINavigationController的实例,使用:

Or if you actually have an instance of UINavigationController, use:

[self setTitle:@"Your Title"];

这篇关于设置导航栏标题的对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆