嗨,对不起,如果这是一个愚蠢的问题。
我一直在关注HackingWithSwift SwiftUI教程第19天(link)但我面临着一个问题。我试图使用.focus()
修饰符和布尔值来处理一个按钮,并在用户按下完成时隐藏我的键盘。每当我试图隐藏我的键盘时,我有2个"完成"按钮,即使我刚刚添加了一个到我的UI,即使我删除了一个按钮,它也不会显示"完成"按钮,我无法隐藏我的键盘。
注意:我在Preview和iPhone模拟器上试用过,但没有在物理设备上试用。
我添加了一个截图和代码以及。
下面是我的代码:
// 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()
}
}
下面是截图:
2条答案
按热度按时间lo8azlld1#
也许问题就在这里:
检验不带括号的:
6g8kf2rb2#
将“工具栏”块移出“表单”。我不知道为什么它能工作,但它确实能工作。Attaching a screenshot of how it looks after the change