0

I want to use a logo as the title of a UINavigationBar globally across all screens in my application. How can I access the navigation bar in AppDelegate?

enter image description here

rebellion
  • 6,628
  • 11
  • 48
  • 79

3 Answers3

1

If you want to affect the whole app you should use appearance proxy in AppDelegate. Something like this should do it:

let navBar = UINavigationBar.appearance()
navBar.setBackgroundImage(UIImage(named: "yourImageName"), forBarMetrics: .Default)
Nikola Milicevic
  • 2,886
  • 3
  • 17
  • 17
0

This is what i have used

add below code in App-delegate's didFinishLaunchingWithOptions method

UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"image_name_here"]];
[imgView setFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2,2,70,14)];
[imgView setCenter:CGPointMake(self.view.center.x,20)];
[self.navigationController.navigationBar addSubview:imgView];

or alternate way is

UIImage *image = [UIImage imageNamed:@"image_name_here"];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image];
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
0

This is impossible. The titleView is always local to a UIViewController.

However, you can:

  1. Create a subclass of UIViewController that will set your self.navigationItem.titleView to that image view and then inherit all your view controllers from this class.

  2. Create a subclass of UINavigationController, giving it a custom navigation bar which will always have the logo displayed inside. Then you will have to use this customized navigation controller instead of UINavigationController.

  3. Set it as navigation bar background image. You can actually make it globally but sometimes it's not an option (especially if you want to keep the default navigation bar appearance).

Of course, if you are using storyboads/xib, you can just put the same UIImageView with the logo everywhere.

Sulthan
  • 128,090
  • 22
  • 218
  • 270