0

I'm doing some changes to the Navigationbar with appearance in appdelegate.

This is my method:

-(void) setAppearance{
    NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
    [titleBarAttributes setValue:[UIFont fontWithName:@"AvantGarde-ExtraLight" size:18] forKey:NSFontAttributeName];
    [titleBarAttributes setValue:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    [[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:6.0/256.0 green:57.0/256.0 blue:84.0/256.0 alpha:1.0]];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

    int borderSize = 3;
    UIImageView *navBorder = [[UIImageView alloc] initWithFrame:CGRectMake(0,
                                                                           41,
                                                                           320,
                                                                           borderSize)];

    navBorder.image = [UIImage imageNamed:@"energy_line"];

    navBorder.tag = 999;


    [[UINavigationBar appearance] addSubview:navBorder];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

I have a method in my appdelegate that sets the window to my first viewController, when i call this method, my navBorder is removed from the navigationbar. I don't understand why this happens, there is no code that changes anything in my navigationbar in the viewcontroller.

- (void)rootView
{
    [self.window setRootViewController:initialViewController];
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Lord Vermillion
  • 5,264
  • 20
  • 69
  • 109

1 Answers1

0

I answered pretty much the same question here: https://stackoverflow.com/a/26414437/538491

But here is a summary: Calling [[UINavigationBar appearance] returns an appearance proxy for the receiver class. The addSubview: method is not tagged as UI_APPEARANCE_SELECTOR. One major downside to UIAppearance's proxy approach is that it's difficult to know which selectors are compatible.

You should get a hold of the navigation bar and add the image there by calling this method: [self.navigationController.navigationBar addSubview:navBorder] or you should subclass UINavigationBar which gives you more flexibility.

Community
  • 1
  • 1
Roberto
  • 3,487
  • 1
  • 22
  • 24