NEW! Master Swift design patterns with my latest book! >>

< Previous: Adding views to UIStackView with addArrangedSubview()   Next: iPad multitasking >

Removing views from a UIStackView with removeArrangedSubview()

That was a long chapter, and I hope you learned a lot. But you deserve a break, so I have some good news: it's trivial to remove views from a UIStackView. Heck, at its simplest it's just a matter of telling removeArrangedSubview() which view you don’t want then removing that view from its superview – the others are automatically resized and re-arranged to fill the space.

In this particular project, we need to do a little more:

  • We want the delete button to work only if there's a web view selected.
  • We want to find the location of the active web view inside the stack view, then remove it.
  • If there are now no more web views, we want to call setDefaultTitle() to reset the user interface.
  • We need to find whatever web view immediately follows the one that was removed.
  • We then make that the new selected web view, highlighting it in blue.

We already pointed the delete button at a method called deleteWebView(), so all you need to do is plug this in. I've added comments to make sure it's all clear:

@objc func deleteWebView() {
    // safely unwrap our webview
    if let webView = activeWebView {
        if let index = stackView.arrangedSubviews.index(of: webView) {
            // we found the current webview in the stack view! Remove it from the stack view
            stackView.removeArrangedSubview(webView)

            // now remove it from the view hierarchy – this is important!
            webView.removeFromSuperview()

            if stackView.arrangedSubviews.count == 0 {
                // go back to our default UI
                setDefaultTitle()
            } else {
                // convert the Index value into an integer
                var currentIndex = Int(index)

                // if that was the last web view in the stack, go back one
                if currentIndex == stackView.arrangedSubviews.count {
                    currentIndex = stackView.arrangedSubviews.count - 1
                }

                // find the web view at the new index and select it
                if let newSelectedWebView = stackView.arrangedSubviews[currentIndex] as? UIWebView {
                    selectWebView(newSelectedWebView)
                }
            }
        }
    }
}

So, although the act of removing a view from a UIStackView is just a matter of calling removeArrangedSubview() and removeFromSuperview(), we need to do a little more to make sure the user interface updates correctly.

You might be wondering why removeFromSuperview() is required when we're already calling removeArrangedSubview(). The reason is that you can remove something from a stack view's arranged subview list then re-add it later, without having to recreate it each time – it was hidden, not destroyed. We don't want a memory leak, so we want to remove deleted web views entirely. If you find your memory usage ballooning, you probably forgot this step!

The last thing we're going to do is talk about multitasking on iPad, and add a few user interface clean ups to make the project complete…

Love Hacking with Swift?

Get all 40 projects in PDF and HTML: buy the Hacking with Swift book! It contains over 1300 pages of hands-on Swift coding, and will really help boost your iOS career

< Previous: Adding views to UIStackView with addArrangedSubview()   Next: iPad multitasking >
MASTER SWIFT NOW
Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Practical iOS 11 Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Click here to visit the Hacking with Swift store >>