How to prevent the keyboard from hiding after pressing a button

41 Views Asked by At

I'm trying to create a screen with a password and confirmation. I added a password reveal feature. What should I do so that after pressing this mask/unmask button, the keyboard doesn't hide but remains open?

    enum UserFormType {
    case password, repeatPassword
    
    var title: String {
        switch self {
        case .password:
            return "Password"
        case .repeatPassword:
            return "Repeat Password"
        }
    }
}

struct UserFormTextField: View {
    @Binding var text: String
    var type: UserFormType
    @State private var isSecure = true
    
    var body: some View {
        VStack(alignment: .leading) {
            if isSecure {
                SecureField("\(type.title)", text: $text)
            } else {
                TextField("\(type.title)", text: $text)
            }
        }
        .font(.body)
        .padding()
        .frame(maxHeight: 60)
        .overlay(
            RoundedRectangle(cornerRadius: 8)
                .stroke(.gray, lineWidth: 2)
        )
        .overlay {
            HStack {
                Spacer()
                Button("", systemImage: isSecure ? "eye.fill" : "eye.slash.fill") {
                    
                    isSecure.toggle()
                }
                .padding(.trailing)
                .tint(.gray)
                .contentTransition(.symbolEffect(.replace))
            }
        }
        
    }
}
0

There are 0 best solutions below