How to hide UINavigationBar 1px bottom line

后端 未结 30 2231
不知归路
不知归路 2020-11-22 08:58

I have an app that sometimes needs its navigation bar to blend in with the content.

Does anyone know how to get rid of or to change color of this annoying little ba

30条回答
  •  旧时难觅i
    2020-11-22 09:17

    Simple solution – Swift 5

    1. Create an extension:

      extension UIImage {
      
          class func hideNavBarLine(color: UIColor) -> UIImage? {
      
              let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
              UIGraphicsBeginImageContext(rect.size)
              let context = UIGraphicsGetCurrentContext()
              context?.setFillColor(color.cgColor)
              context?.fill(rect)
      
      
              let navBarLine = UIGraphicsGetImageFromCurrentImageContext()
              UIGraphicsEndImageContext()
              return navBarLine
          }
      }
      
    2. Add this to viewDidLoad():

      self.navigationController?.navigationBar.shadowImage = UIImage.hideNavBarLine(color: UIColor.clear)
      

提交回复
热议问题