15

I am trying to change the look of the UINavigationBar in my iOS7 app. I am doing the following:

- (void)viewDidLoad
{
    [super viewDidLoad];

    m_sNumberToCall = @"";

    UIBarButtonItem * btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"IconHome.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(btHomeTouched:)];
    self.navigationItem.leftBarButtonItem = btn;

    self.navigationController.navigationBar.translucent = YES;


    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];

    NSShadow * shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
    shadow.shadowOffset = CGSizeMake(0, 1);
    [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0],
                                                           NSForegroundColorAttributeName,
                                                           shadow,
                                                           NSShadowAttributeName,
                                                           [UIFont fontWithName:@"Helvetica-Bold" size:21.0],
                                                           NSFontAttributeName,
                                                           nil]];
}

But, the first time I present the UITableViewController it is the standard iOS7 nav bar, then I press home and present it again and it is my new look.

Any ideas why it does not work the first time?

Itai Spector
  • 652
  • 11
  • 24
LilMoke
  • 3,176
  • 7
  • 48
  • 88
  • try to move code in viewDidAppear – Ilario Nov 14 '13 at 18:40
  • I did try it there and no go, I also tried it in viewWillAppear. – LilMoke Nov 14 '13 at 18:40
  • 1
    For any else who may experience the sam issue, I changed the code from this: [[UINavigationBar appearance] setBa… to this: [self.navigationController.navigationBar setBa… as well as in the setTitleTextAttributes line. Found answer here: http://stackoverflow.com/questions/17361500/how-to-set-navigation-bar-image-ins-ios-7 – LilMoke Nov 14 '13 at 19:01

2 Answers2

28

Don't change the appearance but the navigation bar directly. The appearance affects only the future instances but not the already created ones.

Change:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];

to:

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
Szabi Tolnai
  • 434
  • 5
  • 5
2

The answer before only helps you with the background image but not with the title text attributes.

You don't need to change your code but all you have to do is move it to

applicationDidFinishLaunchingWithOptions

in your AppDelegate.m file.

fabf98dev
  • 53
  • 4