简体   繁体   English

快速删除 UIView 并带回 UIView

[英]Removing a UIView and bringing back the UIView in swift

I am using a UIView not a view controller/ I am removing the view once the delete button is clicked.我使用的是 UIView 而不是视图控制器/单击删除按钮后我将删除视图。 I am trying to make the view come back when a user wants the view back.当用户想要返回视图时,我试图让视图返回。 example : myView.removeFromSuperview() Is there anyway to bring the view back?示例:myView.removeFromSuperview() 有没有把视图带回来? In swift thank you guys!急忙谢谢各位!

@IBOutlet weak var brokeView: UIView!
@IBOutlet weak var deleteButton: UIButton!

@IBAction func deleteViewButton(sender: AnyObject) {
        if brokeView != nil {
            brokeView.removeFromSuperview()
        }
@IBOutlet weak var brokeView: UIView!

you already have a reference to the view brokeView which you are going to remove and add again, but its weak so it will be deallocated once it is removed from superView .你已经拥有的视图的引用brokeView你要删除并重新添加,但其weak所以一旦从删除它会被释放superView Since you need this brokeView to add back make it strong .因为你需要这个brokeView来添加,让它变得strong

@IBOutlet var brokeView: UIView!

Now you can add it back like现在你可以像

view.addSubview(brokeView)

No, there is no way to get it back unless you initialise it again.不,除非您再次初始化,否则无法取回它。 You can hide the view and unhide it.您可以隐藏和取消隐藏视图。

viewToBeHidden.hidden = true
//when you want to make it reappear unhide it.
viewToBeHidden.hidden = false

Or like @rmaddy suggested keep the reference.或者像@rmaddy 建议保留参考。

var myView = UIView()
myView.removeFromSuperview
//then just add it back.
self.view.addSubView(myView)

if you removing it from superview, you can add it again, but you should not make that view as nil other wise you will have to create new one.Removing from superview only remove view from parent view.如果您从超级视图中删除它,您可以再次添加它,但您不应该将该视图设为零,否则您将不得不创建新的视图。从超级视图中删除只会从父视图中删除视图。

subview.removeFromSuperview() // remove from parent view

parentView.addSubview(subview) //adding again on it parent view

Create a View: This sample, it depends on your view which init method, you have created, used that one.创建一个视图:这个示例,它取决于你的视图,你创建了哪个 init 方法,使用了那个。

MyCustomView *customView = MyCustomView(frame: CGRectZero(top: 0, left: 0, width: 200, height: 50)
self.view.addSubview(customView)

If you want to use IBoutlet you need to remove weak from your IBoutlet after you remove weak you remove and add the view again to your view but you need to set the constraints programmatically to view.如果你想使用IBoutlet你需要删除weakIBoutlet删除后weak您删除并重新添加视图到您的视图,但你需要以编程方式设置约束视图。

Example:示例:

// removed 'weak' reference
@IBOutlet var mButton: UIButton!

... other functionality

// remove from superview
mButton.removeFromSuperview()

// add again to view
view.addSubview(mButton)
               
// set re-set constraints 
NSLayoutConstraint.activate([
                    mButton.trailingAnchor.constraint(equalTo:       view.trailingAnchor, constant: -20),
                    mButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -40)
                ])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM