NEW: Join my free 100 Days of SwiftUI challenge today! >>

Final polish

Paul Hudson    @twostraws   

If you try using the app, you’ll soon see it has two problems:

  1. Adding an expense doesn’t dismiss AddView; it just stays there.
  2. When you add an expense, you can’t actually see any details about it.

Before we wrap up this project, let’s fix those to make the whole thing feel a little more polished.

First, dismissing AddView is done by storing a reference to the view’s presentation mode, then calling dismiss() on it when the time is right. The presentation mode is controlled by the view’s environment, and links to the isPresented parameter for our sheet – that Boolean gets set to true by us to show AddView, but will be flipped back to false by the environment when we call dismiss() on the presentation mode.

Start by adding this property to AddView:

@Environment(\.presentationMode) var presentationMode

You’ll notice we don’t specify a type for that – Swift can figure it out thanks to the @Environment property wrapper.

Next, we need to call presentationMode.wrappedValue.dismiss() when we want the view to dismiss itself. This causes the showingAddExpense Boolean in ContentView to go back to false, and hides the AddView. We already have a Save button in AddView that creates a new expense item and appends it to our existing expenses, so add this on the line directly after:

self.presentationMode.wrappedValue.dismiss()

That solves the first problem, which just leaves the second: we show the name of each expense item but nothing more. This is because the ForEach for our list is trivial:

ForEach(expenses.items) { item in
    Text(item.name)
}

We’re going to replace that with a stack within another stack, to make sure all the information looks good on screen. The inner stack will be a VStack showing the expense name and type, then around that will be a HStack with the VStack on the left, then a spacer, then the expense amount. This kind of layout is common on iOS: title and subtitle on the left, and more information on the right.

Replace the existing ForEach in ContentView with this:

ForEach(expenses.items) { item in
    HStack {
        VStack(alignment: .leading) {
            Text(item.name)
                .font(.headline)
            Text(item.type)
        }

        Spacer()
        Text("$\(item.amount)")
    }
}

Now run the program one last time and try it out – we’re done!

LEARN SWIFTUI FOR FREE I have a massive, free SwiftUI video collection on YouTube teaching you how to build complete apps with SwiftUI – check it out!

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.7/5