Keyboard Auto-Hide in iOS

Source: Internet
Author: User

Objective

Many times when we enter information in a text box, we press the confirm or return key to hide the keyboard, or you need to hide the screen after tapping the screen in another blank area.

It is certainly necessary to have the corresponding control respond to the TAP event (click event) so that we can process it.

Two methods of implementing shadowing

There is a concept in iOS called First Responder, which means that the primary responder, the control that is in the focus state on the current screen, is the first responder, and all the gestures we touch on the interface are received by this control. Well understand, when the user clicks on an input box, pop-up input keyboard, this text input box is the first responder, and now to hide the keyboard, in fact, is not let this text input box as the first responder.

The text input box provides a way for us to:Resignfirstresponser

Uitextfield.resignfirstresponder ()
This cancels the first responder identity of the text input box, and the keyboard is automatically hidden.

When there are many input boxes, we can also use UIView to provide a faster method:endediting

Uiview.endediting (True)
Directly to the entire UIView edit state canceled, that no matter which input box on the interface as the first responder identity, can be canceled the editing state, the keyboard disappears.How to Trigger

Knowing how to hide the keyboard is the right time to execute it. There are usually two ways of doing this:

1) The return in the lower right corner of the keyboard, the completion button is clicked

2) When the blank area around the input box is clicked

The first method, you need to handle the didendonexit event of the input box:

@IBAction func txt_didendonexit (sender:uitextfield) {    sender.resignfirstresponder ()}
You can associate the Didendonexit event of n input boxes to this event method, so that you do not have to write duplicate code one after the other.

The second method, when you click on the blank area, the keyboard automatically hide, generally this white space is a uiview, we need to let UIView respond to the tap event, but to know that the response event is Uicontrol , so, We need to change this uiview to Uicontrol, because Uicontrol is a subclass of UIView, so it's totally doable!

By changing the view class to Uicontrol, the view will be able to receive click events like Uicontrol controls.

We handle the Touch down event of this big control:

@IBAction func TouchDown (sender:uicontrol) {    txtname.resignfirstresponder ()}
Note that this is not the sender object, sender is Uicontrol, we are to let the input box to cancel the first responder state.

This is only for the use of ordinary UIView interface, when using utableviewcontroller , you can not use this method, you need to manually create a gesture recognizer, associate it to the entire Uitableviewcontroller .

How to trigger when using Uitableviewcontrol this control

You know, the gesture recognizer can be very flexible to associate to any view, so we can create a uitapgesturerecognizer click Recognizer for Uitableviewcontroller.

In the Viewdidload method, write:

Override Func Viewdidload () {    super.viewdidload ()    var tap = UITapGestureRecognizer (target:self, Action: " Keyboardhide: ")    Tap.cancelstouchesinview = False    Self.view.addGestureRecognizer (tap)}func keyboardhide ( Tapg:uitapgesturerecognizer) {    //entire view  End Edit state (method one)    self.view.endEditing (True)    //Single input box (method two)    Self.txtUserName.resignFirstResponder ()}
So the wholeUitableviewcontrollerThe blank area can receive tap events, it also realizes the function of the hidden input box.

Tips

This article consists of Wp2blog Import, original link: http://devonios.com/ios-hide-keykeyboard.html

Keyboard Auto-Hide in iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.