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