一旦我的WKWebView加载完毕,我就会拉出绝对URL,如果匹配,我会在屏幕上加载一个按钮。
正如预期的那样,在我到达匹配网址之前它不会出现,这就是我想要的行为。
但是,如果我离开页面,按钮仍然存在,我无法理解为什么。
有没有人有任何想法:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if (webView.url?.absoluteString == "https://www.myurl.co.uk/product/clock") {
let settingsButton = UIButton(type: .custom)
view.addSubview(settingsButton)
settingsButton.backgroundColor = UIColor.purple;
settingsButton.translatesAutoresizingMaskIntoConstraints = false
settingsButton.widthAnchor.constraint(equalToConstant: 56).isActive = true
settingsButton.heightAnchor.constraint(equalToConstant: 56).isActive = true
settingsButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -14).isActive = true
settingsButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 14).isActive = true
settingsButton.setNeedsLayout()
settingsButton.layoutIfNeeded()
settingsButton.layer.cornerRadius = 0.5 * settingsButton.bounds.size.width
settingsButton.setImage(UIImage(named:"AR"), for: .normal)
settingsButton.clipsToBounds = true
settingsButton.backgroundColor = UIColor.purple
settingsButton.addTarget(self, action: #selector(ViewController.buttonTapped2(_:)), for: UIControlEvents.touchUpInside)
}else {
settingsButton.removeFromSuperview()
}
答案 0 :(得分:0)
我通过将我的删除方法更改为:
来解决这个问题func RemoveButton() {
for view in self.view.subviews {
if view.isKind(of: UIButton.self) {
view.removeFromSuperview()
}
}