界面随键盘顶起来

在你的代码相关位置添加两个事件
UIKeyboardWillShowNotification,这个是键盘即将弹出的事件通知名称
UIKeyboardWillHideNotification,这个是键盘即将消失的事件通知名称
在这两个通知的回调方法中处理你的工具条动画就行了,键盘的相关参数会在NSNotification实例中的userInfo对象中传过去(键盘的高度、宽度、键盘动画的duration等),你可以在回调函数中根据这些参数处理你的动画,让工具条和键盘动画达到同步的效果。
如果你的系统是ios5,那需要注意一点的就是,不同输入法的键盘高度不同了(可能是216或252),你可能需要根据键盘的具体高度来指定工具条的高度,而不能统一写死为216了。
同时,ios5里面还增加了一个事件通知名称:
UIKeyboardWillChangeFrameNotification
这个通知会在键盘即将改变其大小的时候发出来(比如说键盘弹出、收起、用户切换输入法、用户分享键盘时引起的键盘大小变化),如果你想要你的工具条严格保持在键盘顶部的时候,就需要监听这个事件。

今天在ios5里面偶然发现了问题,补充一下。
UIKeyboardWillChangeFrameNotification
这个通知会在键盘即将改变其大小或位置的时候发出来,包括:
1:键盘弹出、收起。
2:用户切换输入法。
3:用户分割键盘(iPad专有)

4:如果你在viewWillDisappear中让键盘收起(比如说textField resignFirstResponder),键盘似乎不会收起,而只是会改变frame的orig值。

参考以下代码(将self.view换成需要上移的view即可):

-(void)keyboardWillChangeFrame:(NSNotification *)notify{
//NSLog(@"=====%@,%@",notify.object,notify.userInfo);
self.inputView__.backgroundColor=[UIColor groupTableViewBackgroundColor];

NSDictionary *userInfo=notify.userInfo;
NSString *keyboard_frame_begin_NSRectString= [userInfo[UIKeyboardFrameBeginUserInfoKey] description];
NSString *keyboard_frame_begin_CGRectString= nil;
if ([keyboard_frame_begin_NSRectString hasPrefix:@"NSRect"]) {
        keyboard_frame_begin_CGRectString=[keyboard_frame_begin_NSRectString stringByReplacingOccurrencesOfString:@"NSRect"withString:@"CGRect" ];
    }else if( [keyboard_frame_begin_NSRectString hasPrefix:@"CGRect"]){
        keyboard_frame_begin_CGRectString=keyboard_frame_begin_NSRectString;
    }
CGRect keyboard_frame_begin= CGRectFromString(keyboard_frame_begin_CGRectString);



NSString *keyboard_frame_end_NSRectString= [userInfo[UIKeyboardFrameEndUserInfoKey] description];
NSString *keyboard_frame_end_CGRectString= nil;
if ([keyboard_frame_end_NSRectString hasPrefix:@"NSRect"]) {
        keyboard_frame_end_CGRectString=[keyboard_frame_end_NSRectString stringByReplacingOccurrencesOfString:@"NSRect"withString:@"CGRect" ];
    }else if( [keyboard_frame_end_NSRectString hasPrefix:@"CGRect"]){
        keyboard_frame_end_CGRectString=keyboard_frame_end_NSRectString;
    }
CGRect keyboard_frame_end= CGRectFromString(keyboard_frame_end_CGRectString);

CGRect rect=self.view.frame;

CGFloat y_keyboard_begin=keyboard_frame_begin.origin.y;

CGFloat y_keyboard_end=keyboard_frame_end.origin.y;


CGFloat y_current=CGRectGetMinY(rect);
CGFloat y_keyboard_change=y_keyboard_end-y_keyboard_begin;
CGFloat y= y_current +y_keyboard_change;
//NSLog(@"y===========%f",y );


    [UIView animateWithDuration:0.0 animations:^{
self.view.frame=CGRectMake(0, y, CGRectGetWidth(rect), CGRectGetHeight(rect));
    }];


}
原文地址:https://www.cnblogs.com/LiuLady12138/p/4708915.html