Swift addsubview and remove it

前端 未结 5 1275
一向
一向 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 03:46

    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()
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-13 04:01

    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!")
        }
    }
    
    0 讨论(0)
  • 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")
        }
    }
    
    0 讨论(0)
  • 2020-12-13 04:09

    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)

    0 讨论(0)
提交回复
热议问题