FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

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:


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

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) {


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

Hacking with Swift is sponsored by Essential Developer

SPONSORED From August 2nd to 8th you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!

Save your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

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

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.