I'm wondering how to make the keyboard disappear when the user touches outside of a UITextField
.
相关问题
- 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
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Xcode: Is there a way to change line spacing (UI L
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
If the view is embedded at all in a
UIScrollView
then you can use the following:The former will animate the keyboard off screen when the table view is scrolled and the later will hide the keyboard like the stock Messages app.
I see that some people are having issues using the
UITapGestureRecognizer
method. The easiest way that I've accomplished this functionality while still leaving my existing button's tap behavior intact is adding only one line to @Jensen2k 's answer:This allowed my existing buttons to still work without using @Dmitry Sitnikov 's method.
Read about that
property
here (search forCancelsTouchesInView
): UIGestureRecognizer Class ReferenceI'm not sure how it would work with scrollbars, as I see some had issues with, but hopefully someone else might run into the same scenario I had.
Send message
resignFirstResponder
to the textfiled that put it there. Please see this post for more information.Swift 4
Setup your
UIViewController
with this extension method once e.g inviewDidLoad
:and the keyboard will be dismissed even by tapping on the
NavigationBar
.Swift version of @Jensen2k's answer:
One liner
You can do this using the Storyboard in XCode 6 and above:
Create the action to hide the keyboard
Add this to the header file of the class used by your ViewController:
Then add this to the implementation file of the same ViewController:
This will now be one of the 'Received Actions' for your storyboard scene (i.e. ViewController):
Hook up the action to the user event
Now you need to hook up this action to the user gesture of touching off the keyboard.
Important - You need to convert the 'UIView' that's contained in your storyboard to a UIControl, so it can receive events. Select the view from your View Controller Scene hierarchy:
...and change its class:
Now drag from the small circle next to the 'received action' for your scene, onto an 'empty' part of your scene (actually you're dragging the 'Received Action' to the UIControl). You'll be shown a selection of events that you can hook up your action to:
Select the 'touch up inside' option. You've now hooked the IBAction you created to a user action of touching off the keyboard. When the user taps off the keyboard, it will now be hidden.
(NOTE: To hook the action to the event, you can also drag from the received action directly onto the UIControl in your View Controllers hierarchy. It's displayed as 'Control' in the hierarchy.)