Several ways to hide keyboard in iOS

Source: Internet
Author: User

Reprint Learning Http://www.cnblogs.com/jerehedu/p/4496421.html

In iOS development, it is often necessary to enter information. There are two ways of entering information: Uitextfield and Uitextview. After the information input is completed, you need to hide the keyboard, here are a few ways to hide the keyboard.

< a > Click the Return button on the keyboard to hide the keyboard

1, Uitextfield

Compliance Agreement

@interface Viewcontroller () <UITextFieldDelegate>

Set up Proxy

mytextfield.delegate=self;

Using the methods in the proxy

-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField

{

[MyTextField Resignfirstrespinder];

return NO;

}

2, Uitextview

Compliance Agreement

@interface Viewcontroller () <UITextViewDelegate>

Set up Proxy

textview.delegate=self;

Use the methods in the proxy:

-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) text{

if ([text [email protected] "\ n"]) {

[TextView Resignfirstresponder];

return NO;

}

return YES;

}

< two > Add a button on the keyboard, click the button to hide the keyboard

Set button

Uitoolbar *topview=[[uitoolbar Alloc]initwithframe:cgrectmake (0,0,320,30)];

[TopView Setbarstyle:uibarstyleblack];

Uibarbuttoitem *btnspace=[[uibarbuttonitem Alloc]initwihbarbuttonsystemitem:

Uibarbuttonsystemitemflexiblespace target:self Action:nil];

Uibarbuttonitem *donebutton=[[uibarbuttonitem alloc]initwithtitle:@ "Done" style:

Uibarbuttonitemstyledone tsrget:self Action: @swelector (Dismisskeyboard)];

Nsarrar * Buttonarray=[nsarrar Arraywithpbjects:btnspace,donebutton,nil];

[TopView Setitems:buttonarray];

1, Uitextfield

[MyTextField Setinputaccessoryview:topview];

2, Uitextview

[TextView Setinputaccessoryview:topview];

Add a hidden keyboard method

-(void) Dismisskeyboar

{

[MyTextField Resignfirstresponder];

[TextView Resignfirstresponder];

}

< three > Click a blank location other than the keyboard to hide the keyboard.

Let the input control discard the first responder identity in the Touchesbegan:withevent: method

-(void) Touchesbegan: (NSString *) touches withevent: (uievent *) event{

[MyTextField Resignfirstresponder];

[TextView Resignfirstreponder];

}

< four > Add gestures, click a blank location other than the keyboard to hide the keyboard

Add a click gesture

Uitapguesturerecognizer *tap=[[uitapguesturerecognizer alloc]initwithtarget:self Action: @selector (DismissKeyBoard )];

tap.numberoftouchedrequired=1;

tap.numberoftapsrequired=1;

[Self.view Addgesturerecgnizer:tap];

Hide keyboard

-(void) dismisskeyboard{

[MyTextField Resigfirstrespinder];

[TextView Resigfirstrespinder];

}

< five > just click on the blank screen except the keyboard, regardless of which control pops up on the keyboard, the keyboard is hidden

-(void) Touchhesbegan: (Nsset *) touches withevent: (uievent *) event{

[Self.view Endediting:yes];

}

Several ways to hide keyboard in iOS

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.