Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a UIView and bringing back the UIView in swift

Tags:

ios

swift

I am using a UIView not a view controller/ I am removing the view once the delete button is clicked. 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? 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()
        }
like image 612
Frank Boccia Avatar asked Jun 21 '16 17:06

Frank Boccia


People also ask

What does Addsubview do Swift?

Adds a view to the end of the receiver's list of subviews.


1 Answers

@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. Since you need this brokeView to add back make it strong.

@IBOutlet var brokeView: UIView!

Now you can add it back like

view.addSubview(brokeView)
like image 171
Anil Varghese Avatar answered Dec 14 '22 18:12

Anil Varghese