iPhone/Objective-C - Change default "navigationItem.prompt" color
Is there a way to set the self.navigationItem.prompt
color to White? At the moment it's Black.
This is how I'm setting my navigationBar开发者_JAVA百科 color at the moment:
// Set top navigation bar.
UINavigationBar *bar = [navController navigationBar];
[bar setTintColor:[UIColor colorWithRed:180.0/255.0 green:25.0/255.0 blue:34.0/255.0 alpha:1]];
UIImageView *navBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[navBar release];
..and this is how I'm setting my navigationItem.prompt
:
self.navigationItem.prompt = @"Tap image for more options.";
Its possible now in iOS 5 or later. Try this code.
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor, nil]];
I'm afraid that the prompt color is hardcoded and not customizable.
I managed to customize the prompt using the following (tricky) hack, that replace the prompt :
- Set the
navigationItem.prompt = @""
to force the navigationBar to display an empty prompt - Set the
navigationItem.titleView
with :- A
UILabel
or whatever you want to replace title, with inside, as a subView, to replace the prompt :- Another UILabel as prompt, styled as you like
- That label being x centered and with a y=-31px offset (that's where normal prompt is)
- Make sure that the titleView has
.clipsToBounds = NO
so the custom prompt is drawed properly
- A
I applied this for all my controllers by overriding UIViewController.setTitle:
with something like (that won't work/build as is, it is a quick extraction of a more complex code, but that shows the logic) :
- (void)setTitle:(NSString *)title {
[super setTitle:title];
UILabel *titleLabel = ... build the titleLabel
titleLabel.text = title;
if (self.navigationItem.prompt) {
UILabel *promptLabel = ... build the promptLabel
promptLabel.text = self.navigationItem.prompt;
self.navigationItem.prompt = @"";
promptLabel.centerX = titleLabel.width/2;
promptLabel.top = -31;
[titleLabel addSubview:promptLabel];
titleLabel.clipsToBounds = NO;
}
self.navigationItem.titleView = titleLabel;
}
In iOs 5 I changed with appearance of UINavigationBar. You can change color of prompt with following code
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor], UITextAttributeTextColor, [UIColor colorWithRed:10 green:20 blue:30 alpha:1.0f], UITextAttributeTextShadowColor, [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset, nil]];
I used to random number & colors, You can customize colors by passing your own choice. Hope it will help.
try this way.
navigationhBar.tintColor = [UIColor colorWithRed:0.4039216041564941 green:0.6470588445663452 blue:0.062745101749897 alpha:1];
Swift:
self.navigationController?.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName : UIColor.white]
self.navigationController?.navigationBar.barTintColor = UIColor.red
精彩评论