首页 > 解决方案 > 如何使用 swiftUI 更改键盘以反映段选择的输入类型?

问题描述

目前,我可以使用与 SwiftUI 视图上的字母数字或数字键盘输入类型相对应的分段选择器来关闭并显示键盘。我想知道如何在不被解雇和代表的情况下更换键盘。因此,当点击切换时,键盘会自动调整。我正在努力使 UI 更简单,因为它需要用户额外点击,所以存在一个小的不便。

public struct ContentView: View {

@State private var selectedInputType: Int = 0
@State private var keyboardType: KeyboardInputType = .name
@State private var selectedKeyboardType: KeyboardInputType = .name
@State private var inputTypes: KeyboardInputType.AllCases = KeyboardInputType.allCases

public var body: some View {
    Picker(selection: $selectedKeyboardType, label: Text("SomeInputHere")) {
        ForEach(0..<KeyboardInputType.allCases.count) { type in
            Text(self.inputTypes[type].description).tag(type)
        }
    }.pickerStyle(SegmentedPickerStyle())
        
        .onReceive([self.selectedInputType].publisher.first(), perform: { (value) in
            DispatchQueue.main.async {
                self.keyboardType = KeyboardInputType(rawValue: value)!
            }
            print(self.selectedInputType)
        })
}

}

标签: iosswiftui

解决方案


推荐阅读