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?
Putting @alessandro-orru's answer in one extension
then in your view controller just add:
From iOS 13 on, you can use the
UINavigationBarAppearance()
class with theshadowColor
property:Wonderful contributions from @TheoF, @Alessandro and @Pavel.
Here is what I did for...
Using it in
viewDidLoad()
:for Swift 3.0 just change this line:
to this:
Solution for Swift 4.0 - 5.2
Here is small extension for changing both Height and Color of bottom navbar line
And after adding this extension you can call this method on any UiNavController's (e.g. from ViewController
viewDidLoad()
)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.
Option 1: on a single navigation bar
And then in your view controller (change the UIColor to what you like):
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.