【问题标题】:Keyboard dismiss not working properly when dismiss the view swiftui关闭视图swiftui时键盘关闭无法正常工作
【发布时间】:2021-07-06 09:09:49
【问题描述】:

当视图关闭但键盘未正确关闭时,我试图关闭键盘,它们仅隐藏按钮但显示高度。更多细节请查看我的代码和截图。

  1. 键盘显示正常

  1. 键盘打开和关闭时转到下一个屏幕

  1. 键盘按钮隐藏但显示高度与背景

import SwiftUI

struct ContentView: View {
    @State private var titleDtr: String = ""
    @State private var clearAllText: Bool = false
    @State var updateKeyboard = true
    @State var userProfiles = [""]
    
    var body: some View {
        NavigationView{
            HStack{
                CustomTxtfldForFollowerScrn(isDeleteAcntScreen: .constant(false), text:  $titleDtr, clearAllText: $clearAllText, isFirstResponder: $updateKeyboard, completion: { (reponse) in
                    if titleDtr.count >= 3 {
                        userProfiles.append(titleDtr)
                    }else if titleDtr.count <= 3 {
                        userProfiles.removeAll()
                    }
                }).background(Color.red)
                
                VStack(spacing:0) {
                    List {
                        if userProfiles.count > 0 {
                            ForEach(userProfiles.indices, id: \.self) { indexs in
                                NavigationLink(destination: ShowLoadingText()) {
                                    Text(userProfiles[indexs]).foregroundColor(.blue)
                                }
                            }
                        }
                    }
                }
            }.onDisappear{
                updateKeyboard = false
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

import SwiftUI

struct ShowLoadingText: View {
    var body: some View {
        ZStack {
            VStack(alignment:.center, spacing: 15) {
                HStack(spacing:10){
                    Group {
                        ProgressView()
                            .progressViewStyle(CircularProgressViewStyle(tint: Color.white))
                        Text("Loading More Users...")
                    }
                }.foregroundColor(.black)
            }
        }
    }
}




struct CustomTxtfldForFollowerScrn: UIViewRepresentable {
    
    @Binding var isDeleteAcntScreen: Bool
    @Binding var text: String
    @Binding var clearAllText: Bool
    @Binding var isFirstResponder: Bool
    var completion: (String) -> Void
    
    func makeUIView(context: UIViewRepresentableContext<CustomTxtfldForFollowerScrn>) -> UITextField {
        let textField = UITextField(frame: .zero)
        textField.text = text
        textField.delegate = context.coordinator
        textField.backgroundColor = .clear
        if isDeleteAcntScreen {
            textField.placeholder = "DELETE"
        }else{
            textField.placeholder = "Username"
        }
        textField.returnKeyType = .default
        textField.textColor = .black
        return textField
    }
    
    func makeCoordinator() -> CustomTxtfldForFollowerScrn.Coordinator {
        return Coordinator(self)
    }
    
    func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<CustomTxtfldForFollowerScrn>) {
        uiView.text = text
        if isFirstResponder && !context.coordinator.didBecomeFirstResponder  {
            uiView.becomeFirstResponder()
            context.coordinator.didBecomeFirstResponder = true
        }else if clearAllText {
            DispatchQueue.main.async {
                uiView.text! = ""
                text = ""
                clearAllText = false
            }
        }else if !isFirstResponder {
            DispatchQueue.main.async {
                UIApplication.shared.endEditing()
            }
        }
    }
    
    class Coordinator: NSObject, UITextFieldDelegate {
        var didBecomeFirstResponder = false
        var parent: CustomTxtfldForFollowerScrn
        
        init(_ view: CustomTxtfldForFollowerScrn) {
            self.parent = view
        }
        
        func textFieldDidChangeSelection(_ textField: UITextField) {
            DispatchQueue.main.async {
                self.parent.text = textField.text ?? ""
                self.parent.completion(textField.text!)
            }
        }
        
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            textField.resignFirstResponder()
            return true
        }
    }
}

import UIKit

extension UIApplication {
    func endEditing() {
        sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

【问题讨论】:

    标签: swiftui keyboard dismiss swiftui-navigationlink


    【解决方案1】:

    这是关闭键盘的另一种方法。

    首先,移除主队列。

    func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<CustomTxtfldForFollowerScrn>) {
        uiView.text = text
        if isFirstResponder && !context.coordinator.didBecomeFirstResponder  {
            uiView.becomeFirstResponder()
            context.coordinator.didBecomeFirstResponder = true
        }else if clearAllText {
            DispatchQueue.main.async {
                uiView.text! = ""
                text = ""
                clearAllText = false
            }
        }else if !isFirstResponder { // < -- From here
            UIApplication.shared.endEditing()
        }
    }
    

    然后更新updateKeyboard就出现ShowLoadingText()

    ForEach(userProfiles.indices, id: \.self) { indexs in
        NavigationLink(destination: ShowLoadingText().onAppear() { updateKeyboard = false }) { // <-- Here
            Text(userProfiles[indexs]).foregroundColor(.blue)
        }
    }
    
    

    删除onDisappear 代码。

    【讨论】:

      猜你喜欢
      • 2020-02-24
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 2020-05-17
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多