//添加监听事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

//监听回调
#pragma mark - keyboard 
- (void)keyboardWillShow:(NSNotification*)notification {

    CGRect frame = self.view.frame;
    frame.origin.y -= 166;
    //frame.size.height +=216;
    // Shrink view's inset by the keyboard's height, and scroll to show the text field/view being edited
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
    [UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
    self.view.frame = frame;
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification*)notification {

    CGRect frame = self.view.frame;
    frame.origin.y += 166;
    //frame.size.height -=216;
    //self.view移回原位置
    // Restore dimensions to prior size
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
    [UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
    self.view.frame = frame;
    [UIView commitAnimations];
}

 

相关文章:

  • 2022-12-23
  • 2021-10-01
  • 2021-11-12
  • 2021-08-28
  • 2022-12-23
  • 2022-12-23
  • 2021-09-03
  • 2022-01-31
猜你喜欢
  • 2021-04-10
  • 2022-12-23
  • 2021-11-06
  • 2021-09-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案