键盘上升下降
Posted 日月哥哥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了键盘上升下降相关的知识,希望对你有一定的参考价值。
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGFloat height = keyboardRect.size.height;
CGFloat offset = CGRectGetMaxY(self.urlInput.frame) + 10 - (CGRectGetMaxY(self.view.bounds) - height);
NSNumber *durationNum = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
CGFloat duration = [durationNum floatValue];
if (offset > 0)
{
[UIView animateWithDuration:duration animations:^{
self.view.frame = CGRectMake(0,-offset, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
}];
}
}
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
NSDictionary *userInfo = [aNotification userInfo];
NSNumber *durationNum = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
CGFloat duration = [durationNum floatValue];
[UIView animateWithDuration:duration animations:^{
self.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
}];
}
以上是关于键盘上升下降的主要内容,如果未能解决你的问题,请参考以下文章