Answers for "dismiss keyboard swift"

1

how to dismiss keyboard swiftui

#if canImport(UIKit)
extension View {
    func hideKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}
#endif
Posted by: Guest on October-05-2020
1

swift dismiss keyboard on return

extension UIViewController: UITextFieldDelegate{
    public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true;
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    textField.delegate = self
}
Posted by: Guest on March-03-2020
1

how to dismiss keyboard swiftui

struct ContentView: View {
    @State private var tipAmount = ""

    var body: some View {
        VStack {
            TextField("Name: ", text: $tipAmount)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .keyboardType(.decimalPad)

            Button("Submit") {
                print("Tip: \(self.tipAmount)")
                self.hideKeyboard()
            }
        }
    }
}
Posted by: Guest on November-24-2020
0

how to dismiss keyboard in swift

override func viewDidLoad() {
    super.viewDidLoad()

    //Looks for single or multiple taps. 
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")

    //Uncomment the line below if you want the tap not not interfere and cancel other interactions.
    //tap.cancelsTouchesInView = false 

    view.addGestureRecognizer(tap)
}

//Calls this function when the tap is recognized.
@objc func dismissKeyboard() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    view.endEditing(true)
}
Posted by: Guest on March-22-2020
-1

dismiss keyboard when tap outside swift 5

extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false            
        view.addGestureRecognizer(tap)
    }

    @objc func dismissKeyboard() {
        view.endEditing(true)
    }
}

//Now in every UIViewController, all you have to do is call this function:

override func viewDidLoad() {
    super.viewDidLoad()
    self.hideKeyboardWhenTappedAround() 
}
Posted by: Guest on July-01-2020

Code answers related to "dismiss keyboard swift"

Code answers related to "Swift"

Browse Popular Code Answers by Language