Frank Boccia Frank Boccia - 1 year ago 64
iOS Question

Adding UIView back to View

I have multiple subviews in my main view controller, I am using a delete button to remove one subview at a time. I am trying to allow the user to bring back the view that was deleted, but the view is not coming back. Any thoughts? In Swift.

@IBOutlet var tornView: UIView!
var deleted = 1

// Delete Button
@IBAction func deleteViewButton(sender: AnyObject) {
if deleted == 1 {
deleted = 2

// Brings View to Screen
@IBAction func showTornAnnotation(sender: AnyObject) {
if toggleState == 1 {
firstSlider.hidden = false
tornView.hidden = false
toggleState = 2
if deleted == 2 {
else {
firstSlider.hidden = true
tornView.hidden = true
toggleState = 1

Answer Source

If you want IBOutlet to be removed from the superView and get it added back then you should always use strong references to your IBOutlet. Being said you should also keep the position of the removed view so that you can use it when you are ready to add it back.

Edit: Sample code

@IBOutlet var customView: UIView!
var customViewFrame: CGRect?

override func viewDidLoad() {
    customView.backgroundColor = UIColor.blueColor()
@IBAction func remove(sender: AnyObject) {
    customViewFrame = customView.frame

@IBAction func add(sender: AnyObject) {
    if let rect = customViewFrame {
        customView = UIView.init(frame: rect)
        customView.backgroundColor = UIColor.blueColor()