iOS Development Touch TextView edit area off keyboard

Source: Internet
Author: User

Textview,textfield the way to close the keyboard there are many kinds of online,

The most common is the use of the keyboard key to switch off as a signal, and even the worst user interaction listening keyboard input. Then add the Close button on the keyboard, this is not good, not simple enough. There are also add buttons at the bottom of the interface, which belong to adding the Close button type on the interface, and the keyboard pops up to move the entire subview to the top of the keyboard, which is a way I used to subview,subview. But the interface is too many things, the more simple the feeling of the interface is better.

Another way to close this article is to close the keyboard by clicking View outside the edit area, but the user also needs to distinguish between the editing area and the poor interactivity. If the TextView need to cover the entire view, this method is over, because the entire interface in addition to TextView is the keyboard, view can not listen to touch events, the corresponding function will not callback.

This article mainly describes how to edit TextView just click on the TextView itself can close the keyboard, this is the current mainstream way, but on the internet almost do not see the corresponding demon, hereby offer an article, if there is inappropriate, please more advice, learn from each other.

TextView font, keyboard type settings What do not say.

@implementation xxxxviewcontroller{

UITapGestureRecognizer *tgr;//is used to undo the keyboard.

}

First register the keyboard popup close function in Viewwillappear:

-(void) Viewwillappear: (BOOL) animated{

[[Nsnotificationcentor Defaultcenter] addobserver:self selector: @selector (keyboarddidshow:) Name: Uikeyboarddidshownotification Object:nil];

[[Nsnotificationcentor Defaultcentor] addobserver:self selector: @selector (keyboarddidhidden) Name: Uikeyboarddidhidenotification Object:nil];

[Super Viewwillappear:yes];

}
Then add a touch event to the function called after the keyboard popup, because only after the keyboard pop-up need to listen to touch events, the keyboard off the state is not required, iOS default touch textview pop-up keyboard, if you also customize a listening event, the keyboard may not play out.

-(void) Keyboarddidshow: (nsnotification *) notification{
UITapGestureRecognizer *tapgesturerecognizer = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector ( Keyboardhide:)];

Tapgesturerecognizer.canceltouchesinview = NO;

[Self.textview Addgesturerecognizer:tapgesturerecognizer];

TGR = tapgesturerecognizer;//is used to undo touch snooping.

}

-(void) Keyboardhide: (UITapGestureRecognizer *) tap{
[Self.textview Resignfirstresponder];

[Self.textview removegesturerecognizer:tgr];//undo Touch Monitoring, or the next keyboard won't bounce.

}

OK, that's it, the process is like this. Touch TextView, pop-up keyboard (System comes with callback), add touch monitoring, touch textview, turn off the keyboard, Undo touch monitoring, Touch TextView, eject keyboard (System comes with callback) .....

Forwarding indicates the source, thank you.

Http://control.blog.sina.com.cn/admin/article/article_add.php?is_new_editor=1


This article is from the "Dark Night God Cat" blog, please make sure to keep this source http://ikingx.blog.51cto.com/10151780/1783301

iOS Development Touch TextView edit area off 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.