Swift addsubview and remove it

前端 未结 5 1292
一向
一向 2020-12-13 03:15

I want to add sub view and remove with one tap. This is my code:

/* To add subview */

var testView: UIView = UIView(frame: CGRectMake(0, 0, 320, 56         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 04:00

    I've a view inside my custom CollectionViewCell, and embedding a graph on that view. In order to refresh it, I've to check if there is already a graph placed on that view, remove it and then apply new. Here's the solution

    cell.cellView.addSubview(graph)
    graph.tag = 10
    

    now, in code block where you want to remove it (in your case gestureRecognizerFunction)

    if let removable = cell.cellView.viewWithTag(10){
       removable.removeFromSuperview()
    }
    

    to embed it again

    cell.cellView.addSubview(graph)
    graph.tag = 10
    

提交回复
热议问题