IOS7 several ways to hide the keyboard by clicking on the space "Go"

Source: Internet
Author: User
Tags uicontrol

iOS development often use the input box, by default, click on the input box will pop up the keyboard, but you must implement the input box to return the delegate method to cancel the keyboard display, for the user experience is very unfriendly, we can achieve click outside the keyboard blank area to hide the keyboard, Below I have summed up several ways to hide the keyboard:

Let's start by explaining two ways you can hide your keyboard:

1. [View endediting:YES] This method allows the entire view to cancel the first responder, thus allowing the keyboard of all controls to be hidden.

2, [textfiled Resignfirstresponder] This is more commonly used to let a textfiled keyboard hidden.

Here are a few ways to achieve this:

The first: Use the view's Touchesbegan: Touch event to hide the keyboard, which triggers the event when the view area is clicked.

[HTML]View Plaincopyprint?
    1. -(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{
    2. [Textfiled Resignfirstresponder];
    3. }


The second type: Create a custom touch gesture to hide the keyboard:

[HTML]View Plaincopyprint?
  1. -(void) viewdidload
  2. {
  3. [Super Viewdidload];
  4. UITapGestureRecognizer *tapgesturerecognizer = [[UITapGestureRecognizer alloc] initwithtarget:self action:@  Selector (keyboardhide:)];
  5. Set to No to indicate that the current control is propagated to other controls when it responds, by default yes.
  6. Tapgesturerecognizer.cancelstouchesinview = NO;
  7. Add a touch event to the current view
  8. [Self.view Addgesturerecognizer:tapgesturerecognizer];
  9. }
  10. -(void) Keyboardhide: (uitapgesturerecognizer*) tap{
  11. [Textfiled Resignfirstresponder];
  12. }

The Third Kind: Modify the custom class of UIView in Xib to Uicontrol,uicontrol is a kind of common control such as UIButton's parent class, is UIView's derived class, implements the touch and the down-pressing encapsulation.

1, first set xib in the UIView custom class for Uicontrol


2, set the relationship event, drag the Xib UIView into the. h zone

Set the event to touch up Inside

3. Write Hidden code:

[HTML]View Plaincopyprint?
    1. -(Ibaction) TouchView: (ID) Sender {
    2. [Self.view Endediting:yes];
    3. }


Well, the above is three more commonly used methods of hiding the keyboard, each can be used for different occasions and its advantages and disadvantages, see how to use.

(Forwarding Source: http://blog.csdn.net/swingpyzf/article/details/17091567)

IOS7 several ways to hide the keyboard by clicking on the space "Go"

Related Article

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.