Home > Blockchain >  How to close, exit or dismiss Decimal Pad when I click outside a textfield?
How to close, exit or dismiss Decimal Pad when I click outside a textfield?

Time:01-09

My Swift app required the user to enter decimal values on a UITextfield. The Decimal Pad appears when the user click inside the textfield. Then the user enter the amount and click the Calculate Button to see results.

I want to close the Decimal Pad when the user click outside the de UITextfield or outside the keyboard.

My App looks like this: My App

This is my first app in Swift (Xcode). So I'm newbie. Please try to recommend a code.

I am doing something wrong. :(

The error

CodePudding user response:

as comment given by: cristian_064

step: 1 - create IBOutlet of the UIView

@IBOutlet weak var vwParent: UIView!

step: 2 - assign tap gesture to UIView

let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
vwParent.addGestureRecognizer(tap)

step: 2 - create an event handler(@objc) method for handling the operation when UIView is tapped.

@objc func handleTap(_ sender: UITapGestureRecognizer? = nil) {
   self.view.endEditing(true)
}

CodePudding user response:

Just use the IQKeyboardManager library, it will solve all your keyboard problems. A "Close" button will appear above the keyboard, a tap outside the keyboard to hide will also work in any controller without additional settings

  •  Tags:  
  • Related