ios Swift addsubview 并删除它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28197079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift addsubview and remove it
提问by Dasoga
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 Someone can help me please? Thanks!
但是删除它不起作用有人可以帮助我吗?谢谢!
回答by Dasoga
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+
斯威夫特 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!")
}
}
回答by rakeshbs
You have to use the viewWithTag
function to find the view with the given tag
.
您必须使用该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")
}
}
回答by craft
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
方法来删除它
foo.removeFromSuperview()
回答by Pravin Kamble
Tested this code using XCode 8 and Swift 3
使用 XCode 8 和 Swift 3 测试了这段代码
To Add Custom View to SuperView use:
要将自定义视图添加到 SuperView,请使用:
self.view.addSubview(myView)
self.view.addSubview(myView)
To Remove Custom View from Superview use:
要从 Superview 中删除自定义视图,请使用:
self.view.willRemoveSubview(myView)
self.view.willRemoveSubview(myView)