98

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

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Dasoga
  • 5,489
  • 4
  • 33
  • 40
  • What does "not working" mean? The adding? The removing? Errors? Please edit your question to include more information. – ABMagil Jan 28 '15 at 16:15
  • 1
    testView is the view with tag 100, not self.view, so your if statement is never executed. – rdelmar Jan 28 '15 at 16:15

5 Answers5

116

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!")
    }
}
Dasoga
  • 5,489
  • 4
  • 33
  • 40
86

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")
    }
}
Himanshu padia
  • 7,428
  • 1
  • 47
  • 45
rakeshbs
  • 24,392
  • 7
  • 73
  • 63
  • Thanks! actually this solved my problem. But the interaction with de super view it doesn't disable. How can i disable the interaction with the super view? – Dasoga Jan 28 '15 at 17:28
  • I didn't understand. What interaction with superview? – rakeshbs Jan 28 '15 at 17:30
  • I have a tap gesture in the first view, after with a button i add a subview, but is just a information view, so, with a tap a i want to remove it. Now it is working the remove tap. But in the first view i receive the same tap, i need to disable that tap... thanks!! – Dasoga Jan 28 '15 at 17:34
  • Where do you want to tap to remove the information view? Do you want to remove the view when the information view is tapped on the inside and not outside ? – rakeshbs Jan 28 '15 at 17:36
  • Yes, when the user tap on the information view(second view), i want to remove that view(second view) and show the **first** view again. – Dasoga Jan 28 '15 at 17:48
  • Add a uitaprecognizer to the information view and use it to remove the view – rakeshbs Jan 28 '15 at 17:49
9

Assuming you have access to it via outlets or programmatic code, you can remove it by referencing your view foo and the removeFromSuperview method

foo.removeFromSuperview()
craft
  • 2,017
  • 1
  • 21
  • 30
9

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
-11

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)

Ankur Lahiry
  • 2,253
  • 1
  • 15
  • 25
Pravin Kamble
  • 849
  • 8
  • 12