IOS-43-导航栏标题navigationItem.title不能改变颜色的两种解决方法

IOS-43-导航栏标题navigationItem.title不能改变颜色的两种解决方法

IOS-43-导航栏标题navigationItem.title不能改变颜色的两种解决方法

 

两种方法只是形式不一样而已,但是第一种适合在导航栏特别多,而且只需要在被统一继承的基类里面设置即可: 

1.在本类或者所继承的基类重写此方法: 

- (void)setTitle:(NSString *)title; 

见代码:

// 重写set title方法

- (void)setTitle:(NSString *)title {

 

    UILabel *titleLabel         = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];

    titleLabel.text             = title;

    titleLabel.font             = [UIFont boldSystemFontOfSize:20.f];

    titleLabel.textAlignment    = NSTextAlignmentCenter;

    titleLabel.textColor        = [UIColor whiteColor];

    self.navigationItem.titleView = titleLabel;

 

}

 

2.第二种就是直接在viewWillAppear:(BOOL)animated方法 

见代码:

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

 

    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];

    titleLabel.text             = @"换肤";

    titleLabel.font             = [UIFont boldSystemFontOfSize:20.f];

    titleLabel.textAlignment    = NSTextAlignmentCenter;

    titleLabel.textColor        = [UIColor whiteColor];

    self.navigationItem.titleView = titleLab;

    }

posted @ 2019-03-07 09:49  sundaysios  阅读(468)  评论(0编辑  收藏  举报