The first is to get the size of the keyboard, you need to manually call the Registerforkeyboardnotifications method, the other two will be automatically called, pop the keyboard high 216 (input in English),
@implementation Viewcontroller
-(void) viewdidload
{
[Super Viewdidload];
[Self registerforkeyboardnotifications];
Uitextview *TV = [[Uitextview alloc] initwithframe:cgrectmake (100, 100, 100, 50)];
[Self.view ADDSUBVIEW:TV];
[TV release];
}
-(void) registerforkeyboardnotifications
{
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardwasshown:) Name: Uikeyboarddidshownotification Object:nil];
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardwashidden:) Name: Uikeyboarddidhidenotification Object:nil];
}
-(void) Keyboardwasshown: (nsnotification *) Notif
{
Nsdictionary *info = [Notif userInfo];
Nsvalue *value = [info objectforkey:uikeyboardframebeginuserinfokey];
Cgsize keyboardsize = [value cgrectvalue].size;
NSLog (@ "keyboard:%f", keyboardsize.height); 216
Keyboardwasshown = YES;
}
-(void) Keyboardwashidden: (nsnotification *) Notif
{
Nsdictionary *info = [Notif userInfo];
Nsvalue *value = [info objectforkey:uikeyboardframebeginuserinfokey];
Cgsize keyboardsize = [value cgrectvalue].size;
NSLog (@ "Keyboardwashidden keyboard:%f", keyboardsize.height);
Keyboardwasshown = NO;
}
-(void) didreceivememorywarning
{
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
@end
Some keyboard monitoring notifications for the system
-(void) viewdidload
{
[Super Viewdidload];
Increase monitoring to receive messages when the keyboard appears or changes
[[Nsnotificationcenter Defaultcenter] Addobserver:self
Selector: @selector (keyboardwillshow:)
Name:uikeyboardwillshownotification
Object:nil];
Increased monitoring, when the key exits when the message is received
[[Nsnotificationcenter Defaultcenter] Addobserver:self
Selector: @selector (keyboardwillhide:)
Name:uikeyboardwillhidenotification
Object:nil];
}
Called when the keyboard appears or changes
-(void) Keyboardwillshow: (nsnotification *) anotification
{
Get the height of the keyboard
Nsdictionary *userinfo = [Anotification userInfo];
Nsvalue *avalue = [UserInfo Objectforkey:uikeyboardframeenduserinfokey];
CGRect keyboardrect = [Avalue cgrectvalue];
int height = keyboardRect.size.height;
}
Called when the key exits
-(void) Keyboardwillhide: (nsnotification *) anotification
{
}
Sometimes we want to add a Finish button to the keyboard after it's bouncing,the main is by adding a toolbar, the above button can use the system, can also be customized.
Uitoolbar * TopView = [[Uitoolbar alloc]initwithframe:cgrectmake (0, 0, +)]; [TopView setbarstyle:uibarstyleblacktranslucent]; Uibarbuttonitem * Btnspace = [[Uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace Target:self Action:nil]; UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom]; Btn.frame = CGRectMake (2, 5, +); [Btn addtarget:self Action: @selector (Dismisskeyboard) forcontrolevents:uicontroleventtouchupinside]; [Btn setimage:[uiimage imagenamed:@ "Shouqi"] forstate:uicontrolstatenormal]; Uibarbuttonitem *donebtn = [[Uibarbuttonitem alloc]initwithcustomview:btn]; Nsarray * Buttonsarray = [Nsarray arraywithobjects:btnspace,donebtn,nil]; [TopView Setitems:buttonsarray]; [TextField setinputaccessoryview:topview];-(void) dismisskeyboard{ [TextField resignfirstresponder];}
Some problems with iOS keyboard