vara
vara

Reputation: 836

Customize UINavigationBar in IOS without using UINavigationController setting background image not working

I am newbie for IOS. So I have tried to customize the UINavigationBar with out using navigation controller. I have able to change the tile and background color but I have tried to setting the background image programmatically its not working.whether its possible to set background image using story board itself ? I have tried may websites in that mostly used programmatically. so any possible way to set the background image using UIStoryboard ?

Upvotes: 2

Views: 422

Answers (2)

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

we can do like

enter image description here

and you get the output of

enter image description here

Note

if you load the Image from Assests use like UIImage(named: "xx")

if let img = UIImage(named: "xx")
{
    self.topBar.setBackgroundImage( img , forBarMetrics: .Default)
 }

else if you load the Image from resource bundle use like UIImage(named: "xx.extenson (.png )")

 if let img = UIImage(named: "xx.jpg")
{
    self.topBar.setBackgroundImage( img , forBarMetrics: .Default)
 }

you can check with this function in apple douments

enter image description here

here I attached the sample Project link Download and check once

Upvotes: 2

Vishal Sonawane
Vishal Sonawane

Reputation: 2693

You can use

[yourNavigationbar setBackgroundImage:[UIImage imageNamed: @"YourBackgroundImage.png"] 
               forBarMetrics:UIBarMetricsDefault];

For Swift

yourNavigationbar.setBackgroundImage(UIImage(named: "YourBackgroundImage.png"),
               forBarMetrics: .Default)

Upvotes: 0

Related Questions