简体   繁体   English

NavigationBar文本颜色不变吗?

[英]NavigationBar text colour not changing?

In my UITabBar application I am adding NavigationController in each tab. 在我的UITabBar应用程序中,我在每个选项卡中添加NavigationController Suppose the selected index is 0 when I am opening the app.In my 3rd tab(selected index=2) I want to change the text colour of navigationBar title.And I am using the code 假设我打开应用程序时选择的索引为0.在我的第3个选项卡(选择的索引= 2)中,我想更改navigationBar标题的文本颜色。并且我正在使用代码

   [[UINavigationBar appearance] setTitleTextAttributes:
             [NSDictionary dictionaryWithObjectsAndKeys:
              [UIColor greenColor],UITextAttributeTextColor,nil]];

But its not changing the text color of Index 0 and 2.Colour of index 1 will change only if that tab was not clicked before changing the colour by using the above code. 但是,它不会改变索引0和2的文本颜色。仅当在使用上述代码更改颜色之前未单击该选项卡时,索引1的颜色才会改变。 I don't know whats is the issue ? 我不知道是什么问题? Any help/code will be appreciable. 任何帮助/代码都将是可理解的。

Use this to do related Navigationbar title changes: 使用此功能进行相关的Navigationbar标题更改:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Here you can set View width and height as per your requirement for displaying titleImageView position in navigationbar
    UIView *backView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];

    [backView setBackgroundColor:[UIColor clearColor]];
    UILabel *titleLbl =  [[UILabel alloc]initWithFrame:CGRectMake(45, 5, 180,30)];
    [titleLbl setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18.0f]];
    [titleLbl setText:@"Review Her"];
    titleLbl.backgroundColor=[UIColor clearColor];
    [titleLbl setTextColor:[UIColor colorWithRed:0/255.0f green:136/255.0f blue:230/255.0f alpha:1.0f]];
    [backView addSubview:titleLbl];

    self.navigationItem.titleView = backView;
}

A simpler implementation would be to change the navigationBar's tintColor property on the UIViewController that is inside the UINavigationController of interest. 一个更简单的实现是更改目标UINavigationController内的UIViewController上navigationBar的tintColor属性。

In your UIViewController, add the following line in one of the View Lifecycle methods: 在您的UIViewController中,将以下行添加到View Lifecycle方法之一:

self.navigationController.navigationBar.tintColor = [UIColor greenColor];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM