简体   繁体   中英

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

I was just looking for the same thing and found Alex RR's easy solution using iOS 5:

NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIColor whiteColor],UITextAttributeTextColor, 
                                            [UIColor blackColor], UITextAttributeTextShadowColor, 
                                            [NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];

[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];

Reference: iPhone Navigation Bar Title text color

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.

**

在iOS 5之后,只需执行以下操作:

    nav1.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];

In iOS 7, just use:

self.navController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};

Change [UIColor whiteColor] with whatever text color you want.

in iOS 7 this is how you can change the color of Navbar title/text:

UIColor *red = [UIColor colorWithRed:254.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0];

NSMutableDictionary *navBarTextAttributes = [NSMutableDictionary dictionaryWithCapacity:1];

[navBarTextAttributes setObject:red forKey:NSForegroundColorAttributeName ];

self.navigationController.navigationBar.titleTextAttributes = navBarTextAttributes;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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