iOS 键盘在工具栏中显示两个“完成”按钮,但我只有一个 - Swiftui

问题描述 投票:0回答:3

嗨,抱歉,如果这是一个愚蠢的问题。

我一直在关注 HackingWithSwift SwiftUI 教程第 19 天(link) 但我面临一个问题。我尝试使用

.focus()
修饰符和布尔值来处理按钮并在用户按下完成时隐藏我的键盘。每当我试图隐藏我的键盘时,我都会有 2 个“完成”按钮,即使我刚刚在 UI 中添加了一个按钮,即使我删除了该按钮,它也根本不会显示“完成”按钮,而且我无法隐藏我的键盘.

注意:我在 Preview 和 iPhone Simulator 上尝试过,但没有在物理设备上尝试过。

我还添加了屏幕截图和代码。

这是我的代码:

//  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()
    }
}

这是截图:

ios swift iphone xcode swiftui
3个回答
0
投票

也许问题就在这里:

.toolbar(){

测试不带括号:

.toolbar {

0
投票

将“工具栏”块移出“表单”。我不知道为什么它有效,但它确实有效。 Attaching a screenshot of how it looks after the change


0
投票

您应该使用 ToolbarItem(id: ...) 但请注意,它捕获单个状态,因此不可能重置不同的 FocusStates 或知道应该重置哪个字段,或者至少我没有找到方法怎么做。

struct FocusCancellable: ViewModifier {
    func body(content: Content) -> some View {
        content
            .toolbar {
                ToolbarItem(id: "_KeyboardDoneButton", placement: .keyboard) {
                    Button(action: {
                        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
                    }) {
                        Text("Done")
                    }
                }
            }
    }
}

extension View {
    public func focusCancellable() -> some View {
        ModifiedContent(content: self, modifier: FocusCancellable())
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.