SwiftUI,无法在键盘后关闭工作表

时间:2020-10-22 16:15:53

标签: ios swiftui

在SwiftUI中,我像这样打开CommentsView工作表:

@State private var selectedCategory: Category?

Button(category.name) {
    selectedCategory = category
}
.sheet(item: $selectedCategory) { category in
    CommentsView(category: category)
}

CommentsView

struct CommentsView: View {
    @Environment(\.presentationMode) private var presentationMode
    @State private var enteredComment: String = ""
    
    let category: Category
    
    var body: some View {
        VStack {
            TextField("Add a comment", text: $enteredComment)
                .textFieldStyle(RoundedBorderTextFieldStyle())

            Button("Close") {
                presentationMode.wrappedValue.dismiss()
            }
        }
    }
}

问题是:当我专注于文本字段并看到键盘后,我无法解雇CommentsView。在聚焦“关闭”按钮之前,按预期工作。

1 个答案:

答案 0 :(得分:0)

将 .sheet 的位置更改为顶部 VStack/HStack 或 ZStack,而不是按钮视图

3.0.2