向导航栏添加自定义阴影而不删除

Added custom shadow to navigationBar not removing

我已将自定义阴影层添加到导航栏,但现在无法删除。请指导。下面是要添加的代码:

let offset: CGFloat = (self.navigationController?.view.safeAreaInsets.top ?? 20)

shadowView = UIView(frame: CGRect(x: 0, y: -offset,
                                      width: (self.navigationController?.navigationBar.bounds.width)!,
                                      height: (self.navigationController?.navigationBar.bounds.height)! + offset))
shadowView?.backgroundColor = UIColor.white
self.navigationController?.navigationBar.insertSubview(shadowView!, at: 1)

let shadowLayer = CAShapeLayer()
shadowLayer.path = UIBezierPath(roundedRect: shadowView!.bounds, byRoundingCorners: [.bottomLeft, .bottomRight, .topLeft], cornerRadii: CGSize(width: 0, height: 0)).cgPath

shadowLayer.fillColor = UIColor.white.cgColor

shadowLayer.shadowColor = UIColor.darkGray.cgColor
shadowLayer.shadowPath = shadowLayer.path
shadowLayer.shadowOffset = CGSize(width: 2.0, height: 2.0)
shadowLayer.shadowOpacity = 0.8
shadowLayer.shadowRadius = 2

shadowView?.layer.insertSublayer(shadowLayer, at: 0)

对于删除我已经尝试过:

//   self.navigationController?.view.layer.shadowColor = UIColor.clear.cgColor
 //   self.navigationController?.view.layer.shadowOpacity = 0.0
   // self.navigationController?.navigationBar.isTranslucent = true
  //        for view in navigationController?.navigationBar.subviews ?? [] {
 //            if view.tag != 0 {
 //                view.removeFromSuperview()
 //            }
 //        }

self.navigationController?.navigationBar.isHidden = true
   // self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
   // self.navigationController?.navigationBar.shadowImage = UIImage()

self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = .clear

以上所有语句都尝试过但没有成功。请指导相同。谢谢

shadowView.removeFromSuperview()

会成功的。

您是否在不同的文件中添加和删除?

您可以使用:

shadowView.tag = 1001 navigationController?.navigationBar.subviews.first(where: {[=11=].tag == 1001})?.removeFromSuperview()