15

Code below is not working for me, can anyone help to figure it out what is wrong?

var image = UIImage(named: "10384605_10152519403846670_5189785375955620548_n.jpg") as UIImage

self.navigationController.navigationBar.setBackgroundImage(image , forBarMetrics:UIBarMetrics)

11 Answers 11

36
self.navigationController.navigationBar.setBackgroundImage(image, 
                                                   forBarMetrics: .Default)
Sign up to request clarification or add additional context in comments.

3 Comments

How to set image for the back button?
Is it no longer working? I'm trying this code with iOS 12 and Xcode 10 but it's not working. The image is shown for a second and then the NavigationBar turns only white and without the separator line and shadow...
for swift 4.2 => self.navigationController!.navigationBar.setBackgroundImage(UIImage(named: ""), for: .default)
16

In AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    //Image Background Navigation Bar
    let navBackgroundImage:UIImage! = UIImage(named: "backgroundNB.png")
    UINavigationBar.appearance().setBackgroundImage(navBackgroundImage, forBarMetrics: .Default)

    return true
}

Comments

14

In Swift 3:

If you want to add a repeating image in the background you can make this call in AppDelegate > didFinishLaunchingWithOptions:

let image = UIImage(named: "imageNameInAsset")
UINavigationBar.appearance().setBackgroundImage(image, for: .default)

If you want to add an image to the center of the navigation bar you need to do this in the ViewController > viewWillAppear:

let titleView = UIImageView(image: UIImage(named: "imageNameInAsset"))
self.navigationItem.titleView = titleView

Comments

9

If you want to fill the image in navigation bar just use the code:

self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "your_Background_Image_Name")?.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .default)

Comments

3
let navBackgroundImage:UIImage! = UIImage(named: "navbar_bg")

[UINavigationBar .appearance().setBackgroundImage(navBackgroundImage, forBarMetrics:.Default)]

1 Comment

Code only answers tend to be regarded as low quality. Can you edit your answer and add an explanation as how this resolves the issue?
3

For Swift 3:

In AppDelegate.swift:

UINavigationBar.appearance().setBackgroundImage(UIImage(named:"pattern.png"),
                                                                for: .default)

OR

In viewDidLoad():

self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"pattern.png"),
     for: .default)

Comments

2

set backgroundImage in Navigation controller

self.navigationController?.navigationBar .setBackgroundImage(UIImage(named: "cricket"), for: .default)

Comments

1

if You put the navigation bar hidden for the application you have to show it on view did load or appear by :

    override func viewWillAppear(_ animated: Bool) {
    print("\n Debugger : View will appear called")
    self.navigationController?.isNavigationBarHidden =  false
}

if you want your navigation bar only show in a specific view controller you have to disappear the navigation bar by :

override func viewDidDisappear(_ animated: Bool) {
    print("\n Debugger : View did disapper called")

     self.navigationController?.isNavigationBarHidden =  true
}

Navigation bar setup Method

private func navigationBarSetup(){

    print("\n Debugger : Navigation Bar setup method called")

    self.navigationController!.navigationBar.setBackgroundImage(UIImage(named: "your image name "), for: .default)
    let backButton = UIBarButtonItem(image: UIImage(named: "your Image name"), style: .plain, target: self, action: #selector(Your selector method))
    backButton.tintColor = UIColor.white
    self.navigationItem.leftBarButtonItem = backButton
    let rightButton = UIBarButtonItem(image: UIImage(named: "your Image name"), style: .plain, target: self, action: #selector(Your selector method))
    backButton.tintColor = UIColor.white
    self.navigationItem.rightBarButtonItem = rightButton

}

Comments

0

Add the following code to didFinishLaunchingWithOptions method in AppDelegate.swift:

    if let myImage = UIImage(named: "navBarImage.jpg"){
        UINavigationBar.appearance().setBackgroundImage(myImage, for: .default)
    }

Comments

0

@benLIVE asked for how to do a back button, which I was doing when I found the accepted answer, so I thought I'd leave this here too (if a bit late) b/c if you are going to replace a nav bar icon, you may as well replace all of them!

    let cubeIcon = UIImageView(image: yourImage)
    cubeIcon.contentMode = .scaleAspectFit
    self.navigationItem.backBarButtonItem?.image = cubeIcon.image

Comments

0

For those that are using standardAppearance, scrollEdgeAppearance, etc. you may need to do something along the following lines:

let standardAppearance = UINavigationBarAppearance()
standardAppearance.configureWithDefaultBackground()
standardAppearance.backgroundImage = UIImage(/* Your image reference */)
standardAppearance.backgroundImageContentMode = .scaleToFill
UINavigationBar.appearance().standardAppearance = standardAppearance

Using UINavigationBar.appearance().setBackgroundImage(image, for: .default) was not working for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.