我有一个问题,如果我从 super 视图中删除子视图,然后当我推送到另一个VC并重新出现时,所有删除的子视图会重新出现在视图中,我已经尝试了一切并检查了我的代码是否也是viewDidApper。

ios - 即使从 super  View 中删除后,UIView仍会重新出现-LMLPHP

//HERE IS HOW I ADD VIEWS

func addusers() {

    for user in 0...5 {
        let radarButton = PRButton(frame: CGRect(x: 0, y: 0, width: itemSize.width, height: itemSize.height+14))
        radarButton.profileButton?.setImage(UIImage(named: "dummy-avatar.png"), for: UIControlState())
        radarButton.profileName.setTitle("test \(user)", for: .normal)

        repeat {
            let center = generateCenterPointInRadar()
            radarButton.center = CGPoint(x: center.x, y: center.y)

        } while (itemFrameIntersectsInOtherItem(radarButton.frame))
        radarButton.profileButton?.addTarget(self, action: #selector(didTapUser), for: .touchUpInside)
        radarButton.profileName?.addTarget(self, action: #selector(didTapUser), for: .touchUpInside)
        self.addSubview(radarButton)
        items.append(radarButton)
    }

}

//HERE IS HOW I REMOVE VIEWS
func removeAllUsers() {
    for view in self.subviews {
        if view is PRButton  {
            view.removeFromSuperview()
        }
    }
    items.removeAll()

}
//Remove from superview
    override func removeFromSuperview() {

        UIView.beginAnimations("", context: nil)
        UIView.setAnimationDuration(1)
        self.alpha = 0
        UIView.setAnimationDidStop(Selector(("callSuperRemoveFromSuperview")))
        UIView.commitAnimations()

    }

fileprivate func callSuperRemoveFromSuperview() {
    super.removeFromSuperview()
}

提前致谢

最佳答案

首先要看的是...

PRButton中,您将覆盖removeFromSuperview() -但是您的结构不正确,并且您实际上从未删除过该视图。

替换为:

override func removeFromSuperview() {

    UIView.animate(withDuration: 1.0, animations: ({
        self.alpha = 0.0
    }), completion: { _ in
        self.callSuperRemoveFromSuperview()
    })

}

您也可以简单地调用super.removeFromSuperview()而不是其他的self.callSuperRemoveFromSuperview()函数。

了解Debug View Hierarchy功能...您会立即看到的。

10-08 05:30
查看更多