Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change navigation bar bottom border color Swift

It works with

import UIKit  class ViewController: UIViewController {  override func viewDidLoad() {     super.viewDidLoad()     // Do any additional setup after loading the view, typically from a nib.      self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)     self.navigationController?.navigationBar.shadowImage = UIColor.redColor().as1ptImage()   }  override func didReceiveMemoryWarning() {     super.didReceiveMemoryWarning()     // Dispose of any resources that can be recreated. }   }  extension UIColor {     func as1ptImage() -> UIImage {         UIGraphicsBeginImageContext(CGSizeMake(1, 1))         let ctx = UIGraphicsGetCurrentContext()         self.setFill()         CGContextFillRect(ctx, CGRect(x: 0, y: 0, width: 1, height: 1))         let image = UIGraphicsGetImageFromCurrentImageContext()         UIGraphicsEndImageContext()         return image     } } 

But when I add a UITableView it doesn't appear on it and when I add a UISearchView it appears but removes the navigation bar.

Anyone knows how to solve this?

like image 549
TheoF Avatar asked Jul 13 '16 15:07

TheoF


People also ask

How do I customize the navigation bar in Swift?

Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.

What is Scrolledgeappearance?

The appearance settings for the navigation bar when the edge of scrollable content aligns with the edge of the navigation bar. iOS 13.0+ iPadOS 13.0+ Mac Catalyst 13.1+ tvOS 13.0+


2 Answers

You have to adjust the shadowImage property of the navigation bar.

Try this one. I created a category on UIColor as an helper, but you can refactor the way you prefer.

extension UIColor {     func as1ptImage() -> UIImage {         UIGraphicsBeginImageContext(CGSizeMake(1, 1))         let ctx = UIGraphicsGetCurrentContext()         self.setFill()         CGContextFillRect(ctx, CGRect(x: 0, y: 0, width: 1, height: 1))         let image = UIGraphicsGetImageFromCurrentImageContext()         UIGraphicsEndImageContext()         return image     } } 

Option 1: on a single navigation bar

And then in your view controller (change the UIColor to what you like):

// We can use a 1px image with the color we want for the shadow image self.navigationController?.navigationBar.shadowImage = UIColor.redColor().as1ptImage()  // We need to replace the navigation bar's background image as well  // in order to make the shadowImage appear. We use the same 1px color tecnique self.navigationController?.navigationBar.setBackgroundImage(UIColor.yellowColor‌​().as1ptImage(), forBarMetrics: .Default)     

Option 2: using appearance proxy, on all navigation bars

Instead of setting the background image and shadow image on each navigation bar, it is possible to rely on UIAppearance proxy. You could try to add those lines to your AppDelegate, instead of adding the previous ones in the viewDidLoad.

// We can use a 1px image with the color we want for the shadow image UINavigationBar.appearance().shadowImage = UIColor.redColor().as1ptImage()  // We need to replace the navigation bar's background image as well  // in order to make the shadowImage appear. We use the same 1px color technique UINavigationBar.appearance().setBackgroundImage(UIColor.yellowColor().as1ptImage(), forBarMetrics: .Default) 
like image 57
Alessandro Orrù Avatar answered Oct 14 '22 09:10

Alessandro Orrù


Wonderful contributions from @TheoF, @Alessandro and @Pavel.

Here is what I did for...

Swift 4

extension UIColor {      /// Converts this `UIColor` instance to a 1x1 `UIImage` instance and returns it.     ///     /// - Returns: `self` as a 1x1 `UIImage`.     func as1ptImage() -> UIImage {         UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))         setFill()         UIGraphicsGetCurrentContext()?.fill(CGRect(x: 0, y: 0, width: 1, height: 1))         let image = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()         UIGraphicsEndImageContext()         return image     } } 

Using it in viewDidLoad():

/* In this example, I have a ViewController embedded in a NavigationController in IB. */  // Remove the background color. navigationController?.navigationBar.setBackgroundImage(UIColor.clear.as1ptImage(), for: .default)  // Set the shadow color. navigationController?.navigationBar.shadowImage = UIColor.gray.as1ptImage() 
like image 20
backslash-f Avatar answered Oct 14 '22 09:10

backslash-f