Question

On my first view I am displaying a red title in my UINavigationBar. I am using UILabel to achieve this because it is the only way I found to display red text with iOS 7. Now depending on what the user selects, a UIAlertView shows up above the first one (that is consequently becoming the background view). The alert view does not cover the full screen so elements of the first (background) view are still visible. They are all gray except fo the UINavigationBar title text. For consistency I would like the red title to turn gray as well when the alert view shows.

Any idea on how I could change the color at that moment?

My UILabel code:

UILabel *titleView = (UILabel *)self.navigationItem.titleView;
titleView = [[UILabel alloc] initWithFrame:CGRectZero];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0];
//titleView.font = [UIFont fontWithName:@"Arial" size:20.0];
titleView.textColor = [UIColor redColor];

self.navigationItem.titleView = titleView;

self.title = @"View Title";

titleView.text = self.title;
[titleView sizeToFit];

I have also tried to change the color of UINavigationBar title without using UILabel, making it red from the black color of origin without success. UILabel is my back-up. However if there's a way to make it work without UILabel let me know. I have gone through all the related topics on this site without resolution.

Was it helpful?

Solution

[titleView setTextColor:[UIColor redColor]];

Use this right after you show you're alert view.

Example:

if (somethingBecameTrue)
{
    [yourAlertView show];
    [titleView setTextColor:[UIColor redColor]];
}

To turn it back:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    [titleView setTextColor:[UIColor blackColor]];
}

To check wether it's shown or not:

if (!yourAlertView.visible) // Note the exclamation mark (!)
{
    // Alertview is not shown yet
    [yourAlertView show];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top