iOS 7 UINavigationBar appearance not working first time…
Asked Answered
M

2

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?

Morelli answered 14/11, 2013 at 18:17 Comment(3)
try to move code in viewDidAppearHardhack
I did try it there and no go, I also tried it in viewWillAppear.Morelli
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: #17362000Morelli
T
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];
Trim answered 14/11, 2013 at 19:0 Comment(4)
I actually have the same issue and I'm setting appearance before my navigation controller is instantiated...Baguette
That explains a lot !Clemen
The real solution is as answered by @fabf98dev below. You do not need to use navigationController, you just need to make sure that you are calling this line (UINavigationBar.Appear..) before you show your first viewController.Springe
A Great Answer!Marris
T
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.

Trapper answered 27/10, 2014 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.