Uitextfield Click somewhere else to hide the keyboard

Source: Internet
Author: User

In iOS development, the keyboard will pop up when editing the Uitextfield. When editing is complete, you need to hide the keyboard. There are many ways to implement the hidden keyboard, the most common is to TextField FirstResponder resign off. That is [TextField Resignfirstresponder]. This article describes how to hide the keyboard when the keyboard is displayed, tapping the screen, in addition to the keyboard.

The basic ideas are as follows:
1. Add the keyboard display and vanishing notification to the Self.view when the Viewcontroller is loaded.
2. Add and remove Tapgesturerecognizer when keyboard displays and disappears
It's so simple.

The sample code is as follows:
Uiviewcontroller in the source code:

- (void) viewdidload{[Super Viewdidload]; [Self setupfordismisskeyboard];}- (void) Setupfordismisskeyboard {nsnotificationcenter*NC =[Nsnotificationcenter Defaultcenter]; UITapGestureRecognizer*SINGLETAPGR =[[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Tapany  Wheretodismisskeyboard:)]; Nsoperationqueue*mainquene =[Nsoperationqueue Mainqueue]; [NC addobserverforname:uikeyboardwillshownotificationObject: Nil queue:mainquene usingblock:^ (Nsnotification *Note)              {[Self.view ADDGESTURERECOGNIZER:SINGLETAPGR];  }]; [NC addobserverforname:uikeyboardwillhidenotificationObject: Nil queue:mainquene usingblock:^ (Nsnotification *Note)              {[Self.view REMOVEGESTURERECOGNIZER:SINGLETAPGR]; }];}- (void) Tapanywheretodismisskeyboard: (Uigesturerecognizer *) Gesturerecognizer {//This method will resign all Subview first responder in Self.view .[Self.view Endediting:yes];



2 COMMENTS on "iOS development, click on the screen anywhere to hide the keyboard of an implementation scheme"
    1. This is one of the typical practices. Other practices, such as adding uicontrol at the top level after TextField Becomefirstresponder, and then removing the uicontrol when clicked, and field resign.
      But it also has some sequelae. For example, field clicks (such as Select all, move the cursor) and other controls (scroller, drag, etc.) are also masked.
      Therefore, to improve it, it may be necessary to determine in Tapanywheretodismisskeyboard: whether the location of the click should be reflected by the following control (for example, whether the site is contained by a field frame).

      In addition, we often do not want everything to be implemented on the controller, because it will use a duplicate set of code for every place that needs the same effect. It would be much better if it could be implemented with the control itself. So we inherit the Uitextfield and rewrite its-becomefirstresponder and-resignfirstresponder. This may be more comfortable to use.

      However, it is not easy to implement some of the control behaviors that are resign by clicking on the perimeter of the field and want to keep it. Therefore, another option is to define the exact behavior for return key: 1. Jump to the next field,2. Discard FirstResponder.

Uitextfield Click somewhere else to hide the keyboard

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.