简体   繁体   English

UIView甚至在从superview中删除后重新出现

[英]UIView reappear even after begin removed from superview

I have a problem where if I remove subview from super view and then when I push to another VC and come back all the removed subview reappear in the view I have tried everything and checked my code if viewDidApper too. 我有一个问题,如果我从超级视图中删除子视图然后当我推送到另一个VC并返回所有已删除的子视图重新出现在视图中我已经尝试了所有内容并检查了我的代码,如果viewDidApper也是如此。

在此输入图像描述

//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()
}

Thanks in advance 提前致谢

I looked at the test project. 我看了一下测试项目。 I think I found the problem, it was at the override func removeFromSuperview() I comment out it and edit the removeAllUsers() 我想我发现了问题,它是在override func removeFromSuperview()我注释掉它并编辑removeAllUsers()

func removeAllUsers() {
    for view in self.subviews {
        if view is PRButton  {
          UIView.animate(withDuration: 1, animations: { 
            view.alpha = 0
          }, completion: { (finished) in
            view.removeFromSuperview()
          })
        }
    }
    items.removeAll()

}

Now I don't see any duplicate users while I'm back to the viewController 现在,当我回到viewController时,我没有看到任何重复的用户

First thing to look at... 首先要看......

In PRButton you override removeFromSuperview() -- but your structure is incorrect and you never actually remove the view. PRButton您重写removeFromSuperview() - 但您的结构不正确,您实际上从未删除视图。

Replace it with this: 替换为:

override func removeFromSuperview() {

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

}

You could also simply call super.removeFromSuperview() instead of your additional self.callSuperRemoveFromSuperview() function. 您也可以简单地调用super.removeFromSuperview()而不是另外的self.callSuperRemoveFromSuperview()函数。

Get to know the Debug View Hierarchy feature... you would have seen that immediately. 了解Debug View Hierarchy功能......你会立即看到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM