Summary IOS Five ways to turn off keyboard/exit keyboard _ios

Source: Internet
Author: User
Tags touch uicontrol

If you don't say more, then there are several ways to achieve it:

1, click outside the edit area (UIView)

This is a very intuitive way, when no longer need to use the virtual keyboard, just click on the virtual keyboard and edit the area outside, you can put the keyboard, the following code is UIView built in the Touch event method function, you can refer to touch Panel/contact Screen/ The basic use of pressure sensors in the article, find out more about the touch event of the method function.

– (void) touchesended: (Nsset *) touches withevent: (Uievent *) Event {
if (![ Mytextview Isexclusivetouch]) {
[Mytextview resignfirstresponder];

If you want to use this method, please be sure to remember that you operate the picture of Custom Class must be uiview to do.

The Custom Class of the screen is UIView

2. Click outside the editing area (Uicontrol)

The virtual keyboard is put away in the same way as the previous one, but if your touch event is already filled with code, you can consider using the Uicontrol touches up Inside event to close the keyboard by putting the following code with Uicontrol Inside Event link.

– (Ibaction) Dismisskeyboard: (ID) Sender {
[Mytextview resignfirstresponder];

If you want to use this method, please be sure to remember that you operate the picture of Custom Class must be uicontrol to do.
The Custom Class of the screen is Uicontrol

Link the method of the keyboard to the Uicontrol event

3. Use the button that makes the keyboard

When there is no editing area to click to close the keyboard, make a button to put up the current virtual keyboard, but also a good way, because the button must appear in the virtual keyboard to display on the screen, so must borrow Nsnotificationcenter To help us determine the current status of the keyboard.
First in the viewDidLoad: event, the Nsnotificationcenter is registered, telling Nsnotificationcenter our doneButtonshow: method function.

– (void) Viewdidload {
[super viewdidload];
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (donebuttonshow:) Name: Uikeyboarddidshownotification Object:nil];

Now whenever the virtual keyboard appears, it automatically calls our custom doneButtonshow: method function, and then just define how the button appears in the method function.

-(void) Donebuttonshow: (nsnotification *) Notification {
Donebutton = [UIButton buttonwithtype: Uibuttontyperoundedrect];
Donebutton.frame = CGRectMake (0, 228,);
[Donebutton settitle:@ "complete edit" forstate:uicontrolstatenormal];
[Donebutton addtarget:self Action: @selector (Hidekeyboard) forcontrolevents:uicontroleventtouchupinside];

[Self.view Addsubview:donebutton];

Finally, the function of the method when the implementation button is pressed hideKeyboard: , be sure to remember to remove the button from the function.

-(void) Hidekeyboard {
[Donebutton Removefromsuperview];
[Mytextview Resignfirstresponder];


4. Input characters using judgment

If you want to use an input specific character (such as return a newline character) to close the keyboard, you must first use the contract in the section within the category, and @interface you can refer to the use of the Protocol agreement for more information about the agreement.

Following the adoption of the Agreement, the method of expression in the agreement is subsequently implemented textView:shouldChangeTextInRange:replacementText: , this method function is triggered when the character is entered, and the value of the postback BOOL represents whether the character is to be used, and the following code is in this method function to close the virtual keyboard using the way in which the input character is judged (the judge character is return newline character).

– (BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) Text {
if () {
[Mytextview resignfirstresponder];
return NO;
Return YES

Finally, don't forget to viewDidLoad: UITextView point the proxy object to yourself in the event so that the program can correctly find the class that is the actual contract method function.

– (void) Viewdidload {
[super viewdidload];
Mytextview.delegate = self;


5. Questions about the keyboard masking

If you have encountered in the implementation of the keyboard to cover the editing area, you can refer to the use of the keyboard to Animation block the problem of Uitextfield, through the function of the Core Graphic Animation , the keyboard appears in the same time to move the editing area to solve the problem of masking.

6. Summary

These keyboard problems, in the normal development process will encounter, sometimes not good processing. This article is written in great detail, and I hope it will be helpful for everyone's development.

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: 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.