Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift addsubview and remove it

Tags:

ios

swift

uiview

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

like image 525
Dasoga Avatar asked Jan 28 '15 16:01

Dasoga


People also ask

How add and remove Subview in Swift?

addSubview(testView) let aSelector : Selector = "removeSubview" let tapGesture = UITapGestureRecognizer(target:self, action: aSelector) testView. addGestureRecognizer(tapGesture) } func removeSubview(){ println("Start remove sibview") if let viewWithTag = self. view.

What does addSubview do Swift?

Adds a view to the end of the receiver's list of subviews.


2 Answers

Thanks for help. This is the solution: I created the subview and i add a gesture to remove it

@IBAction func infoView(sender: UIButton) {     var testView: UIView = UIView(frame: CGRectMake(0, 0, 320, 568))     testView.backgroundColor = UIColor.blueColor()     testView.alpha = 0.5     testView.tag = 100     testView.userInteractionEnabled = true     self.view.addSubview(testView)      let aSelector : Selector = "removeSubview"     let tapGesture = UITapGestureRecognizer(target:self, action: aSelector)     testView.addGestureRecognizer(tapGesture) }  func removeSubview(){     println("Start remove sibview")     if let viewWithTag = self.view.viewWithTag(100) {         viewWithTag.removeFromSuperview()     }else{         println("No!")     } } 

Update:

Swift 3+

@IBAction func infoView(sender: UIButton) {     let testView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))     testView.backgroundColor = .blue     testView.alpha = 0.5     testView.tag = 100     testView.isUserInteractionEnabled = true     self.view.addSubview(testView)      let aSelector : Selector = #selector(GasMapViewController.removeSubview)     let tapGesture = UITapGestureRecognizer(target:self, action: aSelector)     testView.addGestureRecognizer(tapGesture) }  func removeSubview(){     print("Start remove sibview")     if let viewWithTag = self.view.viewWithTag(100) {         viewWithTag.removeFromSuperview()     }else{         print("No!")     } } 
like image 174
Dasoga Avatar answered Oct 13 '22 00:10

Dasoga


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")     } } 
like image 30
rakeshbs Avatar answered Oct 12 '22 23:10

rakeshbs