在viewdidload之后更改navigationController标题

时间:2014-08-07 12:08:12

标签: ios ios7 uinavigationcontroller uinavigationbar

当我在self.navigationItem.title中设置viewdidload标题正确显示时,稍后根据用户互动我正在更新标题。标题的长度固定为viewdidload中设置的标题的长度。

我希望在我的代码

中发生这种情况
- (void)viewDidLoad 
{
     [super viewDidLoad];
     [self.navigationController.navigationBar setTitleTextAttributes:@{
                                                                  [UIColor brownColor]: NSForegroundColorAttributeName,
                                                                  [UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0]: NSFontAttributeName}];
     self.navigationItem.title = @"My Account";
}


- (void)updateTitleWithUserName {
     self.navigationItem.title = @"Test User 167";
}

结果:

...测试

有任何建议如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

尝试使用标签作为标题,以便您可以在viewDidLoad上调整其大小以适合这样,然后只需在更新时更改tlabel并将其指定给导航标题。 (根据需要管理颜色和字体)

    self.title = @"Your TiTle Text";
    UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];
    tlabel.text=self.navigationItem.title;
    tlabel.textColor=[UIColor whiteColor];
    tlabel.font = [UIFont fontWithName:@"Helvetica-Bold" size: 30.0];
    tlabel.backgroundColor =[UIColor clearColor];
    tlabel.adjustsFontSizeToFitWidth=YES;
    self.navigationItem.titleView=tlabel;

答案 1 :(得分:0)

试试这个

- (void)viewDidLoad 
{
    titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];// create instance variable UILabel * titleLabel;
    titleLabel.text=@"My Account";
    titleLabel.textColor=[UIColor blackColor];
    titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size: 16.0];
    titleLabel.backgroundColor =[UIColor clearColor];
    titleLabel.textAlignment = UITextAlignmentCenter;
    titleLabel.adjustsFontSizeToFitWidth=YES;
    self.navigationItem.titleView= titleLabel;
}
- (void)updateTitleWithUserName 
{
     titleLabel.text = @"Test User 167";
}
相关问题