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, 568))
testView.backgroundColor = UIColor.blueColor()
testView.alpha = 0.5
testView.tag = 100
super.view.userInteractionEnabled = false
self.view.userInteractionEnabled = true
self.view.addSubview(testView)
/* To remove subview */
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject() as UITouch
let point = touch.locationInView(self.view)
if(testView.tag==100){
println("Tag 100")
testView.removeFromSuperview()
}
else{
println("tag not found")
}
}
But the remove it isn't working Someone can help me please? Thanks!
Tested this code using XCode 8 and Swift 3
To Add Custom View to SuperView use:
self.view.addSubview(myView)
To Remove Custom View from Superview use:
self.view.willRemoveSubview(myView)
You have to use the
viewWithTag
function to find the view with the giventag
.Thanks for help. This is the solution: I created the subview and i add a gesture to remove it
Update:
Swift 3+