Swift添加子视图并将其删除

Das*_*oga 66 uiview ios swift

我想添加子视图并通过一次点击删除.这是我的代码:

/*添加子视图*/

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)
Run Code Online (Sandbox Code Playgroud)

/*删除子视图*/

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

}
Run Code Online (Sandbox Code Playgroud)

但删除它不起作用有人可以帮我吗?谢谢!

Das*_*oga 78

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

@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!")
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:

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!")
    }
}
Run Code Online (Sandbox Code Playgroud)


rak*_*hbs 70

您必须使用该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")
    }
}
Run Code Online (Sandbox Code Playgroud)


cra*_*aft 9

假设您可以通过渠道或编程代码访问它,您可以通过引用您的视图fooremoveFromSuperview方法来删除它

foo.removeFromSuperview()
Run Code Online (Sandbox Code Playgroud)


小智 9

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

cell.cellView.addSubview(graph)
graph.tag = 10
Run Code Online (Sandbox Code Playgroud)

现在,在您想要删除它的代码块中(在您的情况下为gestureRecognizerFunction)

if let removable = cell.cellView.viewWithTag(10){
   removable.removeFromSuperview()
}
Run Code Online (Sandbox Code Playgroud)

再次嵌入它

cell.cellView.addSubview(graph)
graph.tag = 10
Run Code Online (Sandbox Code Playgroud)