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:
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)