Clicking in a textfield makes the keyboard appear. How do I hide it when the user presses the return key?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
Try this,
Then, in textField delegate method
First make your file delegate for UITextField
Then add this method to your code.
Also add
self.textField.delegate = self;
Use this
In swift do like this:
First in your
ViewController
implement thisUITextFieldDelegate
For eg.Now add a delegate to a
TextField
in which you want to dismiss the keyboard when return is tapped either inviewDidLoad
method like below or where you are initializing it. For eg.Now add this method.
In
viewDidLoad
declare:[yourTextField setDelegate:self];
Then, include the override of the delegate method:
Ok, I think for a novice things might be a bit confusing. I think the correct answer is a mix of all the above, at least in Swift4.
Either create an extension or use the ViewController in which you'd like to use this but make sure to implement UITextFieldDelegate. For reusability's sake I found it easier to use an extension:
Add the method textFieldShouldReturn (depending on your previous option, either in the extension or in your ViewController)
In your viewDidLoad method, set the textfield's delegate to self
That should be all. Now, when you press
return
thetextFieldShouldReturn
should be called.