1

how do I set an image to be a background of a Navigation controller, like the view, not the bar.

 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"background.jpg"] forBarMetrics:UIBarMetricsDefault];

I tried using that to set it, and even went as far as to change the property to UIViewControllers, but after that. it just spits errors.

jbiser361
  • 949
  • 7
  • 20

2 Answers2

1

In Your AppDelegate in - (void) applicationDidFinishLaunching:(UIApplication *)application method add the following.

[[UINavigationBar appearance] setBackgroundImage:YourImage forBarMetrics:UIBarMetricsDefault];

In Swift

let navBackgroundImage:UIImage! = UIImage(named: "backgroundNB.png")
UINavigationBar.appearance().setBackgroundImage(navBackgroundImage,  forBarMetrics: .Default)
Atmaram
  • 494
  • 4
  • 14
  • so that worked, but how do I scale it down to make the images less pixelated – jbiser361 Sep 12 '19 at 23:37
  • 1
    Try this code snippet with resizingMode : UINavigationBar.appearance().setBackgroundImage(image.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 0, 0), resizingMode: .stretch), for: .default) – Atmaram Sep 13 '19 at 04:38
0

Why do you want to change the background of the Navigation Controller? It handles the transitioning between two controllers that you want to navigate between, it in of itself isn't used as a controller - it's effectively just the navigation bar component. If you want to change the background, change it on the initial and final views.

  • I'm doing it in XIBs not the storyboards, setting the nav controller image sets the image for the whole thing. and that's my end goal yk – jbiser361 Sep 12 '19 at 01:37
  • @jbiser361 https://stackoverflow.com/questions/45186312/how-do-i-create-a-custom-navigationcontroller-with-image-and-name –  Sep 12 '19 at 06:55