2018-02-26键盘弹起笔记

键盘弹起的常用的有俩个key:
UIKeyboardWillShowNotification //键盘弹起
UIKeyboardWillHideNotification //键盘隐藏

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

还有一个键盘在变化时候的key

  UIKeyboardWillChangeFrameNotification //键盘Frame发生改变
  
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector
   (keyboardWillChange:)name:UIKeyboardWillChangeFrameNotification
   object:nil];

获取键盘的高度:

#pragma mark Keyboard
 -(void)keyboardWillShow:(NSNotification*)notif{
// 1.取出键盘的frame
 CGRect endRect = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] 
  CGRectValue];
//键盘高度
CGFloat keybordHeight = CGRectGetMinY(endRect);
// 2.取出键盘弹出的时间
CGFloat duration = [notif.userInfo [UIKeyboardAnimationDurationUserInfoKey] 
doubleValue];
showDuration = duration;
showKeybordHeight = keybordHeight;}

-(void)keyboardWillHide:(NSNotification*)notif{
// 1.取出键盘弹出的时间
CGFloat duration = [notif.userInfo[UIKeyboardAnimationDurationUserInfoKey] 
doubleValue];
[UIView animateWithDuration:duration animations:^{
   tabView.frame  =  CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT-250);
   tabView.transform = CGAffineTransformIdentity;
}];}

-(void)keyboardWillChange:(NSNotification * )noti{
CGRect changeRect = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] 
CGRectValue];
NSString * str =NSStringFromCGRect(changeRect);
NSLog(@"changeRect==%@",str);
}

打印获取到的rect

  NSString * str =NSStringFromCGRect(endRect);
  NSLog(@"endRect==%@",str);

再根据获取的到键盘的frame属性后进行相应的视图偏移。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容