Some problems with iOS keyboard

Source: Internet
Author: User

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

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.