UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

[SOLVED, but not working] Day 17: 'Done' button does not appear

Forums > 100 Days of SwiftUI

Howdy,

Doing Day 17 & like others there are some minor differences in the code from when Paul produced the video, but I think we can all manage to make it through along with the help found here.

My code ended up running fine, however the Done button never appeared

 .navigationTitle("We Split")
            .toolbar {
                ToolbarItemGroup(placement: .keyboard) {
                    Spacer()
                    Button("Done") {
                        amountIsFocused = false

Everything else worked as intended including the decimal keyboard appearing.

Thank you!

2      

Hey @Baron_Blackbird. I just went back and looked at mine (in Xcode 14.3.1 on Ventura 13.4.1, Simulator 14.3.1) and it looks fine. A couple of things to maybe check:

  1. Are you looking for it in the correct place? Sounds silly but worth double checking. In the Simulator if you toggle the keyboard off (cmd-K) it will just be a small blue "Done" in the bottom right. With the keyboard toggled on, it will appear in the top right of the keyboard area above the number 3.
  2. Have you got your toolbar modifier in the right place? I feel like in the screenshot you have included, and also the (lack of) coloring of the ".toolbar" modifier, I wonder if it is in the correct place in the View's body? If you are unsure you can go in the menu Editor > Structure > Re-Indent (I think for the selection). In my source code toolbar is directly below navigationTitle at the same level of indentation. Yours looks quite different and might be indicative of the issue?

Al

2      

Howdy,

Something else is going on & maybe it is the iPhone 14 Pro Max or ???, because I downloaded Paul's solution from the HWS+ page & ran it locally...exact same results as what I observed & for the curious here is a bit more detail of what is happening.

I came back & when I fired up xcode again the coloring & indenting was appropriate...I don't know why it wasn't the first time.

I don't like putting in a huge chunk of code, but a new & interesting wrinkle appeared...

The DONE button still does not appear in the Simulator...but it does in the Preview! AND, if that wasn't enough...the keyboard doesn't appear in the preview, but it does in the Simulator...

I would have written it all off as a bug or something which will be fixed in the next patch, except...science!

//  Created by Baron Blackbird on 7/15/23.
//

import SwiftUI

struct ContentView: View {
    @State private var checkAmount = 0.0
    @State private var numberOfPeople = 2
    @State private var tipPercentage = 20
    @FocusState private var amountIsFocused: Bool

    let tipPercentages = [10, 15, 20, 25, 0]

    var totalPerPerson: Double {
        let peopleCount = Double(numberOfPeople + 2)
        let tipSelection = Double(tipPercentage)

        let tipValue = checkAmount / 100 * tipSelection
        let grandTotal = checkAmount + tipValue
        let amountPerPerson = grandTotal / peopleCount

        return amountPerPerson
    }

    var body: some View {
        NavigationView {
            Form {
                Section {
                    TextField("Amount: ", value: $checkAmount, format: .currency(code: Locale.current.currency?.identifier ?? "USD"))
                        .keyboardType(.decimalPad)
                        .focused($amountIsFocused)

                    Picker("Number of People", selection: $numberOfPeople) {
                        ForEach(2..<100) {
                            Text("\($0) people")
                        }
                    }
                }

                Section {
                    Picker("Tip Percentage", selection: $tipPercentage) {
                        ForEach(tipPercentages, id: \.self) {
                            Text($0, format: .percent)
                        }
                    }
                    .pickerStyle(.segmented)
                } header: {
                    Text("Percentage to Tip")
                }

                Section {
                    Text(totalPerPerson, format: .currency(code: Locale.current.currency?.identifier ?? "USD"))
                }
            }
            .navigationTitle("We Split")
            .toolbar {
                ToolbarItemGroup(placement: .keyboard) {
                    Spacer()
                    Button("Done") {
                        amountIsFocused = false
                    }
                }
            }
        }
    }
}

#Preview {
    ContentView()
}

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.