skip to Main Content

Hi and sorry if its a dumb question.

I have been following HackingWithSwift SwiftUI tutorial day 19(link)
but I am facing a problem. I tried to use .focus() modifier and a boolean to handle a button and hide my keyboard when user press done. Whenever I am trying to hide my keyboard I have 2 ‘Done’ buttons even though I just added one to my UI and even when I remove that one button it wont show no "Done" button at all and I can’t hide my keyboard.

Note: I tried it on Preview and iPhone Simulator but not on a physical device.

I added a screenshot and code as well.

Here is my code:

//  ContentView.swift
//  WeSplit


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]
    
    //For Total cost + tip
    var grandTotal: Double{
        let tipSelection = Double(tipPercentage)
        let tipValue = checkAmount / 100 * tipSelection
        let grandTotal = checkAmount + tipValue
        
        return grandTotal
    }
    
    
    //For individual share
    var totalPerPerson: Double{
        let peopleCount = Double(numberOfPeople + 2)
        let amountPerPerson = grandTotal / peopleCount
    
        return amountPerPerson
    }
    
    
    
    var body: some View {
        NavigationView {
            Form{
                Section{
                    TextField("Amount: ", value: $checkAmount, format: .currency(code: Locale.current.currencyCode ?? "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("How much tip do you want to leave?")
                }
                
                
                //Grand Total
                Section{
                    Text(grandTotal, format:.currency(code: Locale.current.currencyCode ?? "USD"))
                }header: {
                    Text("Total Cost + Tip")
                }
                
                
                //Showing each person's share
                Section{
                    Text(totalPerPerson, format: .currency(code: Locale.current.currencyCode ?? "USD"))
                }header: {
                    Text("Amount Per Person")
                }.navigationTitle("WeSplit")
                    .toolbar(){
                        ToolbarItemGroup(placement: .keyboard){
                            
                            Button("Done"){
                                amountIsFocused = false
                            }
                        }
                    }
                
                
                
            
            }
        }
    }
        
}



struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Here is the screenshot:

ScreenShot of my ios simulator

2

Answers


  1. Maybe the problem is here:

    .toolbar(){
    

    Test that without parenthesis:

    .toolbar {
    
    Login or Signup to reply.
  2. Move the "toolbar" block out of "Form". I don’t know why it works but it does. Attaching a screenshot of how it looks after the change

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search