【问题标题】:SwiftUI TextEditor how Hide KeyboardSwiftUI TextEditor 如何隐藏键盘
【发布时间】:2021-01-05 18:41:06
【问题描述】:

TextEditor 有问题,在 TextEditor 上编辑文本后无法隐藏键盘。

    @State var monTexte: String = "
var body: some View {
        VStack {
            Spacer()
                .frame(height :15)
                .clipped()
            Text("Project ")
                .font(Font.system(size: 39.00))              
               .fontWeight(.black)
                .foregroundColor(Color.white)
                .multilineTextAlignment(.center)
                .padding(.all, 16.0)
                .clipped()
            
            TextEditor(text: $monTexte)
                .keyboardType(.alphabet)
                .font(.subheadline)
                .padding(.horizontal)
                .font(Font.system(size: 38.00))
                .frame(minWidth: 10, maxWidth: .infinity, minHeight: 10, maxHeight: 200, alignment: .topLeading)
                .border(Color.black)
                .clipped()
}
}
} 

我找到了一种使用文本字段而不是使用 TextEditor 来隐藏键盘的方法 请你帮帮我吧

【问题讨论】:

  • 如何定义编辑后的时刻,即。您想在哪个事件中隐藏键盘?
  • 用特定按钮或使用键盘上的回车键(例如),但没有找到隐藏键盘的功能
  • TextEditor 中的Keybord Return 会生成新行(它不是TextField),但对于某些按钮,您可以使用stackoverflow.com/questions/58349955/… 中的方法。

标签: swiftui text-editor


【解决方案1】:

感谢“Asperi”,它可以使用以下代码:

var body: some View {
        VStack {
            Spacer()
                .frame(height :15)
                .clipped()
            Text("Project ")
                .font(Font.system(size: 39.00))              
               .fontWeight(.black)
                .foregroundColor(Color.white)
                .multilineTextAlignment(.center)
                .padding(.all, 16.0)
                .clipped()
                        HStack {
                Spacer()
                Button("Close Keyboard") {
                    UIApplication.shared.endEditing()

                }.font(Font.system(size: 20))
                .foregroundColor(Color.blue)
                
            }.clipped()
            TextEditor(text: $monTexte)
                .keyboardType(.alphabet)
                .font(.subheadline)
                .padding(.horizontal)
                .font(Font.system(size: 38.00))
                .frame(minWidth: 10, maxWidth: .infinity, minHeight: 10, maxHeight: 200, alignment: .topLeading)
                .border(Color.black)
                .clipped()
}
}
} 

//----------------------------------------------------//
// Masquer le clavier
//----------------------------------------------------//
extension UIApplication {
    func endEditing() {
        sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}```

【讨论】:

    猜你喜欢
    • 2022-11-10
    • 2019-10-22
    • 2023-01-27
    • 2020-09-11
    • 2022-09-29
    • 2021-09-16
    • 1970-01-01
    • 2021-02-01
    • 2023-01-09
    相关资源
    最近更新 更多