In the viewdidiload method
[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (keyboardwillshow :) name: uikeyboardwillshownotification object: Nil];
[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (keyboardwillhide :) name: uikeyboardwillhidenotification object: Nil];
In addition to the two commonly used keyboard methods, you also need to add a method to use when the keyboard is switched to a Chinese Input, because the input box will have one more line of Chinese selection,
# Ifdef _ iphone_5_0
Float version = [[[uidevice currentdevice] systemversion] floatvalue];
If (version> = 5.0 ){
[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (keyboardframedidchange :) name: uikeyboarddidchangeframenotification object: Nil];
}
# Endif
The above is the method to be called. You can write a method for keyboardframedidchange, or use keyboardwillshow, write one more method to enable people who take over to understand what we are doing with this method,
The following three methods are implemented: (I have not deleted some comments of the Code, which is the process of analyzing and trying)
-(Void) keyboardframedidchange :( nsnotification *) Note
{
// Nsdictionary * userinfo = [Notification userinfo];
// Nsvalue * avalue = [userinfo objectforkey: uikeyboardframeenduserinfokey];
// Cgrect keyboardrect = [avalue cgrectvalue];
// Nsvalue * animationdurationvalue = [userinfo objectforkey: uikeyboardanimationdurationuserinfokey];
// Nstimeinterval animationduration;
// [Animationdurationvalue getvalue: & animationduration];
Cgsize size = [[uiscreen mainscreen] bounds]. size; // obtain the screen size;
Cgrect rect = [Note. userinfo [uikeyboardframeenduserinfokey] cgrectvalue]; // obtain the keyboard height;
Cgfloat ty = size. Height-rect. Size. height ;//
[Uiview animatewithduration: [Note. userinfo [uikeyboardanimationdurationuserinfokey] doublevalue] animations: ^ {
// Frame. Size. Height-= rect. Size. height;
If (rect. Origin. y! = Size. Height ){
Cgrect frame = self. tablechat. frame;
Frame. size. height = size. height-rect. size. height-40; // The screen height minus the keyboard height minus the input box height, which is the height of the new table and then assigned to the table,
[Self. bottombar setframe: cgrectmake (0, ty-40, 320, 40)];
[Self. tablechat setframe: frame];
}
Else {
Cgrect frame = self. tablechat. frame;
Frame. Size. Height = size. Height-40;
[Self. bottombar setframe: cgrectmake (0, size. Height-40, screenwidth, 40)];
}
// If (self. bottombar. Frame. Origin. Y = size. Height-40 ){
// [Self. bottombar setframe: cgrectmake (0, ty-40, 320, 40)];
//}
// Else {
// [Self. bottombar setframe: cgrectmake (0, size. Height-40, screenwidth, 40)];
//}
Nsindexpath * indexpath = [nsindexpath indexpathforrow: arraymessagesframe. Count-1 insection: 0];
If (arraymessagesframe. Count = 0 ){
Return;
}
Else
{
[Self. tablechat scrolltorowatindexpath: indexpath atscrollposition: uitableviewscrollpositionbottom animated: Yes];
}
}];
}
-(Void) keyboardwillshow :( nsnotification *) Note {
Cgsize size = [[uiscreen mainscreen] bounds]. size;
Cgrect rect = [Note. userinfo [uikeyboardframeenduserinfokey] cgrectvalue];
Cgfloat ty = size. Height-rect. Size. height;
[Uiview animatewithduration: [Note. userinfo [uikeyboardanimationdurationuserinfokey] doublevalue] animations: ^ {
Cgrect frame = self. tablechat. frame;
// Frame. Size. Height-= rect. Size. height;
Frame. Size. Height = size. Height-rect. Size. Height-40;
[Self. tablechat setframe: frame];
[Self. bottombar setframe: cgrectmake (0, ty-40, 320, 40)];
Nsindexpath * indexpath = [nsindexpath indexpathforrow: arraymessagesframe. Count-1 insection: 0];
If (arraymessagesframe. Count = 0 ){
Return;
}
Else
{
[Self. tablechat scrolltorowatindexpath: indexpath atscrollposition: uitableviewscrollpositionbottom animated: Yes];
}
}];
}
# The Pragma mark keyboard is about to exit
-(Void) keyboardwillhide :( nsnotification *) Note {
Cgsize size = [[uiscreen mainscreen] bounds]. size;
Cgrect rect = [Note. userinfo [uikeyboardframeenduserinfokey] cgrectvalue];
Cgfloat ty = size. Height-rect. Size. height;
[Uiview animatewithduration: [Note. userinfo [uikeyboardanimationdurationuserinfokey] doublevalue] animations: ^ {
Cgrect frame = self. tablechat. frame;
Frame. Size. height + = rect. Size. Height-60;
[Self. tablechat setframe: frame];
[Self. bottombar setframe: cgrectmake (0, size. Height-40, 320, 40)];
Nsindexpath * indexpath = [nsindexpath indexpathforrow: arraymessagesframe. Count-1 insection: 0];
If (arraymessagesframe. Count = 0 ){
Return;
}
Else
{
[Self. tablechat scrolltorowatindexpath: indexpath atscrollposition: uitableviewscrollpositionbottom animated: Yes];
}
}];
}
# Pragma mark-text box proxy method
# Pragma mark click the press enter button on the textfield keyboard
-(Bool) textfieldshouldreturn :( uitextfield *) textfield {
Nsstring * content = textfield. text;
If ([content isinclutostring: @ ""]) {
Return no;
}
[Self addmessagewithcontent: content type: messagetypeme];
Nsuserdefaults * Pref = [nsuserdefaults standarduserdefaults];
Nsstring * struid = [Pref stringforkey: @ "uid"];
// Nsstring * strfrom = @ "Andy ";
// Nsstring * strto = @ "Andy ";
Nsstring * strmsg = [nsstring stringwithformat: @ "{\" Type \ ": \" chat \ ", \" from \ ": \" % @\", \ "to \": \ "% @ \", \ "MSG \": \ "% @ \"} ", struid, self. MSG. strcontact, content];
Nslog (@ "input message: % @", strmsg );
[[Nsicationcenter center defacenter center] postnotificationname: @ "appsendmessage" Object: strmsg];
[Self. tablechat reloaddata];
Nsindexpath * indexpath = [nsindexpath indexpathforrow: arraymessagesframe. Count-1 insection: 0];
[Self. tablechat scrolltorowatindexpath: indexpath atscrollposition: uitableviewscrollpositionbottom animated: Yes];
Self. messagefield. Text = nil;
Return yes;
}