0

I am trying to set a background image for UINavigationBar. I have done it many times in Objective-C but in swift, I am facing a problem. I have searched and tried many things but none did work for me.

Here is the code I am using:

let navBackgroundImage:UIImage! = UIImage(named: "header")
self.navigationController?.navigationBar.setBackgroundImage(navBackgroundImage, for: .default)

My header image has following dimensions:

375 × 64 for 1x and so on for 2x and 3x. I have tried with 320x64 as well, but still, it doesn't work.

Screenshot:

enter image description here As you can see, the image is appearing twice and not covering complete width.

Any suggestions?

EDIT

After trying

self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "header")?.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 0, 0), resizingMode: .stretch), for: .default)

enter image description here Still appearing twice.

iBug
  • 2,334
  • 3
  • 32
  • 65
  • Check this link: https://stackoverflow.com/questions/26052454/ios-8-navigationbar-backgroundimage – 3stud1ant3 Jul 31 '17 at 09:00
  • I don't see any problem in above way of setting background image to the navigation bar. Internally the image should be stretched and displayed accordingly. Why don't you try setting some other image as it could be some issue with the image you tried ? – Shreekara Jul 31 '17 at 09:03

4 Answers4

2

Try this,

Swift

self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "header")?.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 0, 0), resizingMode: .stretch), for: .default)

Objective C

[self.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:@"header"] resizableImageWithCapInsets: UIEdgeInsetsMake(0, 0, 0, 0) resizingMode: UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault];
Faris Muhammed
  • 970
  • 13
  • 17
harshal jadhav
  • 5,456
  • 2
  • 18
  • 26
0

Try print something or use debug point to sure that your code not called twice, or use

let navBackgroundImage:UIImage! = UIImage(named: "header")!.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch)
UINavigationBar.appearance().setBackgroundImage(navBackgroundImage, for: .default)
0

First add this line in your appDelegate didFinishLaunchingWithOptionsMethod

UINavigationBar.appearance().barTintColor = UIColor.clear

Then add the following in viewDidLoad method

if let image = UIImage(named: "header") {
            UINavigationBar.appearance().setBackgroundImage(image.resizableImage(withCapInsets:UIEdgeInsets.zero, resizingMode: .stretch), for: .default)
            UINavigationBar.appearance().contentMode = .scaleAspectFill
}
3stud1ant3
  • 3,586
  • 2
  • 12
  • 15
0

You should use a debug point to make sure the code does not run twice, then:

let navBackgroundImage = UIImage(named: "header")!.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch)
navigationController?.navigationBar.setBackgroundImage(navBackgroundImage, for: .default)
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
H.zh
  • 1