界面随键盘顶起来

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

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

4:若是你在viewWillDisappear中让键盘收起(好比说textField resignFirstResponder),键盘彷佛不会收起,而只是会改变frame的orig值。app

 

参考如下代码(将self.view换成须要上移的view便可):ide

 

-(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));
    }];


}
相关文章
相关标签/搜索