Swift 添加子视图并将其删除

Swift addsubview and remove it

我想一键添加和删除子视图。 这是我的代码:

添加子视图:

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)

删除子视图:

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")
    }
}

但是删除不起作用

您必须使用 viewWithTag 函数来查找具有给定 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")
    }
}

感谢您的帮助。这是解决方案: 我创建了子视图并添加了一个手势来删除它

@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!")
    }
}

更新:

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!")
    }
}

使用 XCode 8 和 Swift 3

测试了此代码

要将自定义视图添加到 SuperView,请使用:

self.view.addSubview(myView)

要从 Superview 中删除自定义视图,请使用:

self.view.willRemoveSubview(myView)

假设您可以通过出口或程序代码访问它,您可以通过引用您的视图 fooremoveFromSuperview 方法

来删除它
foo.removeFromSuperview()

我在我的自定义 CollectionViewCell 中有一个视图,并在该视图上嵌入了一个图表。为了刷新它,我必须检查该视图上是否已经放置了一个图形,将其删除然后应用新的。这是解决方案

cell.cellView.addSubview(graph)
graph.tag = 10

现在,在您要删除它的代码块中(在您的例子中是 gestureRecognizerFunction)

if let removable = cell.cellView.viewWithTag(10){
   removable.removeFromSuperview()
}

再次嵌入

cell.cellView.addSubview(graph)
graph.tag = 10