代码之家  ›  专栏  ›  技术社区  ›  fs_tigre

使用小数的TextFields当在SwiftUI中使用普通键盘从字段移动时,键盘不滚动到可见区域

  •  0
  • fs_tigre  · 技术社区  · 4 年前

    知道为什么吗 Field 3 Field 5 如果前一个字段使用的是普通键盘,则在激活时不可见?

    在以下代码中,如果您点击 Field 1 在你点击后 字段3 字段5 ,它们将不可见;它们被键盘遮住了。

    请注意 字段3 字段5 使用 decimalPad 键盘,而其余字段使用标准键盘。

      struct TextFieldScrollingIssue: View {
        @State private var testInput:String = ""
        
        var body: some View {
            VStack{
                Form {
                    TextField("Field 1", text:$testInput)
                    Spacer()
                    Spacer()
                    Spacer()
                    Spacer()
                    Spacer()
                    Spacer()
                    Spacer()
                    Section(header: Text("Section 1: ")) {
                        TextField("Field 2", text:$testInput)
                        TextField("Field 3", text:$testInput)
                            .keyboardType(.decimalPad)
                    }
                    
                    Section(header: Text("Section 2: ")) {
                        TextField("Field 4", text:$testInput)
                        TextField("Field 5", text:$testInput)
                            .keyboardType(.decimalPad)
                    }
                }
            }
        }
    }
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   Yrb    4 年前

    我认为滚动机制很混乱,因为您对所有 TextFields 。显然,在生产中,你永远不会有这种情况。修复方法很简单,使用不同的变量:

    struct TextFieldScrollingIssue: View {
        @FocusState var isFocused: String?
        @State private var testInput:String = ""
        @State private var decimalInput:String = ""
        
        var body: some View {
            VStack{
                ScrollViewReader { scroll in
                    Form {
                        TextField("Field 1", text:$testInput)
                            .id("Field 1")
                            .focused($isFocused, equals: "Field 1")
                        Text(isFocused?.description ?? "nil")
                        Spacer()
                        Spacer()
                        Spacer()
                        Spacer()
                        Spacer()
                        Spacer()
                        Section(header: Text("Section 1: ")) {
                            TextField("Field 2", text:$testInput)
                                .id("Field 2")
                                .focused($isFocused, equals: "Field 2")
                            TextField("Field 3", text:$decimalInput)
                                .id("Field 3")
                                .focused($isFocused, equals: "Field 3")
                                .keyboardType(.decimalPad)
                        }
                        
                        Section(header: Text("Section 2: ")) {
                            TextField("Field 4", text:$testInput)
                                .id("Field 4")
                                .focused($isFocused, equals: "Field 4")
                            TextField("Field 5", text:$decimalInput)
                                .id("Field 5")
                                .focused($isFocused, equals: "Field 5")
                                .keyboardType(.decimalPad)
                        }
                    }
                    .onChange(of: isFocused) { _ in
                        if let isFocused = isFocused {
                            DispatchQueue.main.async {
                                withAnimation {
                                    scroll.scrollTo(isFocused)
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    

    编辑:

    根据这一评论,我得以复制。已编辑代码以使用 ScrollviewReader , @FocusState 并查看要更正的id。