How to Add Background Image on iPhone Navigation Bar

How to add background image on iphone Navigation bar?

Here's the code from the link @luvieere mentioned.
Paste this code into to the rootview controller just above
@implementation rootviewController

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:@"NavigationBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

As of iOS 5, there is an official way to do this. (see iOS Developer Library)

// someplace where you create the UINavigationController
if ([navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
UIImage *image = [UIImage imageNamed:@"NavigationBar.png"];
[navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}

But still, retain the old code for backward compatibility unless you really want to ditch iOS 4 and below.

Changing the UINavigationBar background image

Starting in iOS 5 you should use the -setBackgroundImage:forBarMetrics: method:

[myNavbar setBackgroundImage:[UIImage imageNamed: @"UINavigationBarBackground.png"] 
forBarMetrics:UIBarMetricsDefault];

And in Swift 4:

navigationBar.setBackgroundImage(UIImage(named: "UINavigationBarBackground.png"),
for: .default)

iOS 8 NavigationBar BackgroundImage

I found the solution. I needed to use the method resizableImageWithCapInsets:resizingMode: and set the resizingMode to UIImageResizingModeStretch, otherwise the image would still tile in the navigation bar.

Objective-C:

[[UIImage imageNamed:@"nav-image-portrait"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];

Swift 3 / 4:

UINavigationBar.appearance().setBackgroundImage(UIImage(named: "image")!.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .default)

Navigation Bar Background Image Does not Extend To Search Bar

There are two problems with that code. First, stop hacking the search bar:

self.searchController.searchBar.barTintColor = UIColor.clear // no
self.searchController.searchBar.backgroundImage = UIImage() // no
searchController.searchBar.backgroundColor = UIColor.clear // no
searchController.searchBar.barStyle = .blackTranslucent // no

Second, stop hacking the navigation bar: Delete the GradientView entirely from your code.

class GradientView: UIView { // no

Just use the tools that the framework gives you. Set the navigation bar's background image (to an image of a gradient) and stop. The search controller's search bar will automatically integrate itself correctly into the navigation bar:

Sample Image

EDIT Okay, so it turns out that this is issue is iOS 13 only. That's because what you're doing is not how to add a background image to a navigation bar in iOS 13. You will have to bifurcate your code:

    if #available(iOS 13.0, *) {
let app = UINavigationBarAppearance()
app.backgroundImage = im // the gradient image
self.navigationController?.navigationBar.scrollEdgeAppearance = app
self.navigationController?.navigationBar.standardAppearance = app
} else {
// your old code goes here
}

How to change the background image of navigation bar with animation?

You need to call layoutIfNeeded() within the animation block e.g.

UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: { 
self.navigationController?.navigationBar.barTintColor = .blue
self.navigationController?.navigationBar.layoutIfNeeded()
}, completion: nil)

Update for changing the background image

This code worked for me:

let animation = CATransition()
animation.duration = 0.5
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
animation.type = kCATransitionFade

navigationController?.navigationBar.layer.add(animation, forKey: nil)

UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "your-image-name"), for: .default)
}, completion: nil)

custom background image with large titles NavigationBar in iOS 11

Finally I found solution!

Edit: Works on iOS 13 and higher


You can use it before view appears, eg: in viewDidLoad() method:

    override func viewDidLoad()
{
super.viewDidLoad()

let largeTitleAppearance = UINavigationBarAppearance()

largeTitleAppearance.configureWithOpaqueBackground()
largeTitleAppearance.backgroundImage = UIImage(named: "BackgroundImage.png")

self.navigationBar.standardAppearance = largeTitleAppearance
self.navigationBar.scrollEdgeAppearance = largeTitleAppearance
}

All that you need is:

  1. Create UINavigationBarAppearance instance:

    let largeTitleAppearance = UINavigationBarAppearance() 

    Apple documentation:

    UINavigationBarAppearance - An object for customizing the appearance of a navigation bar.



  1. Configure it:

    largeTitleAppearance.configureWithOpaqueBackground()

    "Opaque" here because we want to set colorised image (but in practice it doesn't matter, what configure will you set)



  1. Set background image:

    largeTitleAppearance.backgroundImage = UIImage(named: "BackgroundImage.png") // Set here image that you need


  1. Assign our largeTitleAppearance object to both standardAppearance and scrollEdgeAppearance navigationBar's fields:

    self.navigationBar.standardAppearance = largeTitleAppearance // For large-navigationBar condition when it is collapsed
    self.navigationBar.scrollEdgeAppearance = largeTitleAppearance // For large-navigationBar condition when it is expanded

    Apple documentation:

    .standardAppearance - The appearance settings for a standard-height navigation bar.

    .scrollEdgeAppearance - The appearance settings to use when the edge of any scrollable content reaches the matching edge of the navigation bar.


This helped to me: https://sarunw.com/posts/uinavigationbar-changes-in-ios13/#going-back-to-old-style

how to put background image for navigation bar?

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"background.png"] forBarMetrics:UIBarMetricsDefault];


Related Topics



Leave a reply



Submit