swift 如何在隐藏键盘时排除TextField点击

o7jaxewo  于 2023-08-02  发布在  Swift
关注(0)|答案(2)|浏览(142)

目前,我隐藏键盘时,用户点击任何地方。我想排除2 TextField s点按以避免键盘隐藏/显示效果。

import SwiftUI

struct ContentView: View {

    @FocusState var focus:FocusedField?
    @State var name = ""
    @State var email = ""
    @State var phoneNumber = ""

    var body: some View {
        List {
            TextField("Name:",text: $name)
                .focused($focus, equals: .name)
                .onSubmit {
                    focus = .email
                }
            TextField("Email:",text: $email)
                .focused($focus, equals: .email)
                .onSubmit {
                    focus = .phone
                }
            TextField("PhoneNumber:", text: $phoneNumber)
                .focused($focus, equals: .phone)
                .onSubmit {
                    if !name.isEmpty && !email.isEmpty && !phoneNumber.isEmpty {
                        submit()
                    }
                }
        }
        .onTapGesture {
            if (focus != nil) {
                hideKeyboard()
            }
        }
    }

    private func submit() {
        print("submit")
    }
    
    enum FocusedField: Hashable {
        case name, email, phone
    }
}

extension View {
    func hideKeyboard() {
        print("hideKeyboard")
        let resign = #selector(UIResponder.resignFirstResponder)
        UIApplication.shared.sendAction(resign, to: nil, from: nil, for: nil)
    }
}

字符串
我想要:
1.一个稳定的键盘为我的TextField s没有隐藏和显示效果。
1.轻按任意位置以关闭/隐藏键盘。
请查看以下效果:
x1c 0d1x的数据

2uluyalo

2uluyalo1#

自iOS 16以来,有一个定义键盘解除的修改器:

func scrollDismissesKeyboard(_ mode: ScrollDismissesKeyboardMode) -> some View

字符串
您可能需要immediately模式。

var body: some View{
    List {
        // etc.  
    }
    .scrollDismissesKeyboard(.immediately)
}


这并不是在点击时关闭键盘,而是在滚动时关闭键盘,这是典型的iOS行为。

piok6c0g

piok6c0g2#

我找到了答案。

public struct TapToHideKeyboardView: View {
    
    public init() {
    }
    
    public var body: some View {
        Button(action: {
            self.hideKeyboard()
        }) {
            Rectangle().foregroundColor(.brown)
        }
        .background(
            GeometryReader { geometry in
                Color.clear
                    .frame(width: geometry.size.width, height: geometry.size.height)
            }
                .onTapGesture {
                    self.hideKeyboard()
                }
        )
    }
    
    func hideKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

字符串
在Zstack中使用这个公共结构。

var body: some View{
        NavigationStack{
            ZStack {
                TapToHideKeyboardView()
                VStack{
                    TextField("Name:",text:$name)
                        .focused($focus, equals: .name)
                        .onSubmit {
                            focus = .email
                        }
                    TextField("Email:",text:$email)
                        .focused($focus,equals: .email)
                        .onSubmit {
                            focus = .phone
                        }
                    TextField("PhoneNumber:",text:$phoneNumber)
                        .focused($focus, equals: .phone)
                        .onSubmit {
                            if !name.isEmpty && !email.isEmpty && !phoneNumber.isEmpty {
                                submit()
                            }
                        }
                }
            }
        }
    }

相关问题