Swift addsubview and remove it

前端 未结 5 1289
一向
一向 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:09

    You have to use the viewWithTag function to find the view with the given tag.

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        let touch = touches.anyObject() as UITouch
        let point = touch.locationInView(self.view)
    
        if let viewWithTag = self.view.viewWithTag(100) {
            print("Tag 100")
            viewWithTag.removeFromSuperview()
        } else {
            print("tag not found")
        }
    }
    

提交回复
热议问题