Hussain Mansoor
Hussain Mansoor

Reputation: 3124

NavigationBar background image for iOS 10 XCode 8.1 Swift 3.0

How to set the background image for this specific settings? iOS10 and XCode8.1? I tried everything below:

UINavigationBar.appearance().setBackgroundImage(UIImage(named: "navigationBg")!.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 0, 0), resizingMode: .stretch), for: .default)
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "navigationBg")! , for: .any, barMetrics: .default)
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "navigationBg")! , for: .default)

I'm able to set the color of navigationBar using storyboard but unable to set the image.

Upvotes: 3

Views: 9696

Answers (5)

Mrugesh Tank
Mrugesh Tank

Reputation: 3560

Here is the code which perfectly work for me in Swift 3.
Put this in didFinishLaunchingWithOptions.

let image = bg_banner // let image = #imageLiteral(resourceName: "bg_banner")
UINavigationBar.appearance().setBackgroundImage(image.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 0, 0), resizingMode: .stretch), for: .default)

And here is the output which I received. enter image description here

Upvotes: 7

Ashok R
Ashok R

Reputation: 20766

XCode 8.1 Swift 3.0 iOS 10

from Apple documentation:

You can set a custom background image for your navigation bar. You can do this using setBackgroundImage:forBarMetrics:. Note that you must specify bar metrics because navigation bars have different dimensions on different devices and orientations.

let image = UIImage(named: "navigationBg")!
if let navigationController = self.navigationController {
   navigationController.navigationBar.setBackgroundImage(image, for: .default)
}

Reference: Navigation Bars

Upvotes: 1

KAR
KAR

Reputation: 3361

For setting up the background image to whole navigation bar use this cde in swift 3.0,

UINavigationBar.appearance().setBackgroundImage(UIImage(named: "Exam-104")!.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 0, 0)), for: .default)

Navigation bar image

Hope this will help you.

Upvotes: 3

Pragnesh Vitthani
Pragnesh Vitthani

Reputation: 2540

To set NavigationBar Background Image you can write this:

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

Upvotes: 4

Dileep
Dileep

Reputation: 2425

Trying setting "self.navigationItem.titleView" property

Reference : how-to-put-an-image-as-the-navigation-bar-title

Update:

Try this, instead of setting in UINavigationBar()

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

Upvotes: 7

Related Questions