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属性后进行相应的视图偏移。

你可能感兴趣的:(2018-02-26键盘弹起笔记)