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?
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.
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+
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 } }
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)
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)
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With