您的位置:首页 > 移动开发 > IOS开发

文本框随键盘动态改变位置(获取键盘弹起高度)

2016-03-16 09:55 441 查看
- (void)dealloc
{
//释放
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)registerForKeyboardNotifications
{
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];

//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}

#pragma mark - 键盘高度动态改变留言框位置
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
CGRect keyBoardRect=[aNotification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat deltaY=keyBoardRect.size.height;

[UIView animateWithDuration:[aNotification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
_bgView2.transform=CGAffineTransformMakeTranslation(0, -(deltaY+40));
}];
}
//当键盘退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
[UIView animateWithDuration:[aNotification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
_bgView2.transform = CGAffineTransformIdentity;
}];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息