0

I am currently adding a shadow to my navigation bar using the following code:

self.navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0.0, height: 4.0)
self.navigationController?.navigationBar.layer.shadowRadius = 4.0
self.navigationController?.navigationBar.layer.shadowOpacity = 0.3
self.navigationController?.navigationBar.layer.masksToBounds = false

This produces the following result: enter image description here The shadow is being shown correctly, however I do not want the 1px black border of the navigation bar to be shown, I only want the navigation bar to have a shadow. I know that setting the shadow property of the navigation bar to an empty image removes the border, but that also hides the shadow. Is there any way to achieve a shadow without a border?

TOPCOD3R
  • 226
  • 1
  • 3
  • 11
  • Check [this answer](https://stackoverflow.com/a/33477140/4539192). – Rikh Sep 18 '20 at 11:04
  • This hides the shadow as well as the border. I'm looking for a way to show a shadow without a border. – TOPCOD3R Sep 18 '20 at 11:13
  • Mhm I thought since you are changing the image and not shadow property, that would work! My bad – Rikh Sep 18 '20 at 11:44
  • What if you hide the shadow this way: `navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")` – Starsky Sep 18 '20 at 13:42

1 Answers1

2

add this extension:

extension UIView {
fileprivate var hairlineImageView: UIImageView? {
    return hairlineImageView(in: self)
}

fileprivate func hairlineImageView(in view: UIView) -> UIImageView? {
    if let imageView = view as? UIImageView, imageView.bounds.height <= 1.0 {
        return imageView
    }

    for subview in view.subviews {
        if let imageView = self.hairlineImageView(in: subview) { return imageView }
    }
    return nil
  }
}  

Add this line after nav bar shadow in viewWillAppear:

navigationController?.navigationBar.hairlineImageView?.isHidden = true

IF YOU WANT TO SEE THAT THE LINE IS GONE comment nav bar shadow and set table view separator color to clear...

//navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
//navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0.0, height: 4.0)
//navigationController?.navigationBar.layer.shadowRadius = 4.0
//navigationController?.navigationBar.layer.shadowOpacity = 0.3
//navigationController?.navigationBar.layer.masksToBounds = false
    
navigationController?.navigationBar.hairlineImageView?.isHidden = true
tableView.separatorColor = .clear

enter image description here

Fabio
  • 5,432
  • 4
  • 22
  • 24
  • For the second part, I am getting the error "Value of type 'UINavigationBar' has no member 'hairlineImageView'". – TOPCOD3R Sep 18 '20 at 13:40
  • Hi, I just checked again and there is still the same error. There is no hairlineImageView property under the navigationBar. Also, I am trying to show a shadow but only a shadow, right now with the code I have the shadow Is showing but with the 1px line underneath. – TOPCOD3R Sep 18 '20 at 13:56
  • @TOPCOD3R you're right, i forgot to put in the extension... I update my answer, sorry... – Fabio Sep 18 '20 at 13:57
  • Thanks for your answer, but this just hides the 1px line. Is there any way to add a shadow now? I would like a shadow but without the line if it is possible. – TOPCOD3R Sep 18 '20 at 14:20
  • @TOPCOD3R Uncomment your shadow... the second part is only for show that the line is gone... – Fabio Sep 18 '20 at 14:21
  • @TOPCOD3R Have a nice WE :) – Fabio Sep 18 '20 at 14:28