Search code examples
iphoneobjective-ciosuinavigationbaruicolor

how to change front color of navigation bar title


I am using navigation controller in my application and want to change title color of navigationBar.

I am doing so using below code

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor],UITextAttributeTextColor,nil];
    [self.navController.navigationBar setTitleTextAttributes:dic];

One more thing I am using ARC xcode 4.2 and this code is placed on appdelegate only

It is working fine in ios 4+ but not working on below versions.

Please help me how to do this from single code on appdelegate


Solution

  • You can recreate the titleView like that in your viewcontroller:

    UILabel * titleView = [[UILabel alloc] initWithFrame:CGRectZero];
    titleView.backgroundColor = [UIColor clearColor];
    titleView.font = [UIFont boldSystemFontOfSize:20.0];
    titleView.shadowColor = [UIColor colorWithWhite:1.0 alpha:1.0];
    titleView.shadowOffset = CGSizeMake(0.0f, 1.0f);
    titleView.textColor = [UIColor redColor]; // Your color here
    self.navigationItem.titleView = titleView;
    [titleView sizeToFit];
    [titleView release];
    

    the other parameters are those wich are used in the native titleView.

    **

    Please look at Max Strater's answer below to have a more recent solution.

    **