向导航栏添加了自定义阴影,但未删除

问题描述 投票:0回答:1

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

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

以上所有陈述都尝试过,但没有成功。请同样指导。谢谢

ios iphone uinavigationbar swift5 rightbarbuttonitem
1个回答
0
投票

shadowView.removeFromSuperview()

将解决问题。

您是否要在其他文件中添加和删除?

您可以使用:

shadowView.tag = 1001 navigationController?.navigationBar.subviews.first(where: {$0.tag == 1001})?.removeFromSuperview()

© www.soinside.com 2019 - 2024. All rights reserved.