Uitextview automatically hides the keyboard and automatically hides the keyboard

Source: Internet
Author: User
Uitextview automatically hides the keyboard and reads many well-known posts. I am not satisfied with this. Most of them write their own buttons and do not achieve automatic hiding. Here I just took a break and realized the animation of the QQ chat window. In the header file, we define a uitextview nametextfield in. in the M file, implement-(bool) textfieldshouldbeginediting :( uitextfield *) textfield; // return no to disallow editing. -(void) textfielddidbeginediting :( uitextfield *) textfield; // became first responder-(bool) textfieldshouldendediting :( uitextfield *) textfield; these three methods are used to control the content Effect of uitextview and then implement the following three touch event methods-(void) touchesbegan :( nsset *) touches withevent :( uievent *) event;-(void) touchesmo Ved :( nsset *) touches withevent :( uievent *) event;-(void) touchesended :( nsset *) touches withevent :( uievent *) event; to control the re-painting and hiding of the keyboard, you must note that the touch method can only occur in the uiview. Therefore, you must define a uiview to capture the touch, set this view to hidden. It is displayed when it is needed and does not need to be hidden. To achieve the expected results. Here is part of the Demo code:-(void) loadview {// View table uitableview * tableview = [[uitableview alloc] initwithframe: cgrectmake (0.0f, 0.0f, 3200000f, 323.0f) style: uitableviewstyleplain]; tableview. delegate = self; tableview. datasource = self; tableview. separatorstyle = uitableviewcellseparatorstylenone; // cell style. Do not separate tableview. backgroundcolor = [uicolor clearcolor]; // [uicolor colorwithred: 0.859f Green: 0.886f Blue: 0.929f ALPHA: 1.0f]; tableview. tag = tableviewtag; [self. view addsubview: tableview]; [tableview release]; // capture the mask layer of a touch event. uiview * myview1 = [[uiview alloc] initwithframe: tableview. frame]; myview1.backgroundcolor = [uicolor clearcolor]; // set to transparent myview1.tag = myviews; [self. view addsubview: myview1]; [myview1 release];} // when you start editing, enable the mask layer to capture touch events. In this case, uitableview is an invisible layer-(bool) textfieldshouldbeginediting :( uitextfield *) textfield {// return no to disallow editing. uiview * Col = (uiview *) [self. view viewwithtag: myviews]; [col sethidden: No];}-(void) touchesbegan :( nsset *) touches withevent :( uievent *) event {// when a touch event is captured, cancel the first corresponding [nametextfield resignfirstresponder] of uitextfield; // re-paint the work area uitoolbar * toolbar = (uitoolbar *) [self. view viewwithtag: toolbartag]; toolbar. frame = cgrectmake (0.0f, 323.0f, 320366f, 44.0f); // re-paint the uitextfeild control uitableview * tableview = (uitableview *) [self. view viewwithtag: tableviewtag]; tableview. frame = cgrectmake (0.0f, 0.0f, 320366f, 323.0f);}-(void) touchesmoved :( nsset *) touches withevent :( uievent *) event {// set uiview to hide uiview * Col = (uiview *) [self. view viewwithtag: myviews]; [col sethidden: Yes];}-(void) touchesended :( nsset *) touches withevent :( uievent *) event {// set uiview to hide uiview * Col = (uiview *) [self. view viewwithtag: myviews]; [col sethidden: Yes];}

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.