NEW: Learn SwiftUI with my free YouTube video series! >>

< 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.firstIndex(of: webView) {
            // we found the current webview in the stack view! Remove it from the stack view

            // now remove it from the view hierarchy – this is important!

            if stackView.arrangedSubviews.count == 0 {
                // go back to our default UI
            } 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? WKWebView {

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…

SPONSOR Meet the new Instabug – more than just bug reporting! We help you build better apps and minimize your debugging time. With each bug report, we automatically capture details like network requests, repro steps, and session details. Get real-time crash reports with stack trace details and session data to help you catch and fix issues easily. And with our customizable in-app surveys, you’ll gather insightful user feedback and much more. Instabug is the fastest and easiest way to release with confidence. Start your free trial now! Start your free trial now!

< Previous: Adding views to UIStackView with addArrangedSubview()   Next: iPad multitasking >
Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns 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 Advanced iOS Volume Two 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!

Average rating: 3.5/5

Click here to visit the Hacking with Swift store >>