您的位置:首页 > 产品设计 > UI/UE

键盘增加监听,UIButton随键盘位移

2015-12-29 15:04 597 查看
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];

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

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height = keyboardRect.size.height;
[UIView animateWithDuration:0.3 animations:^{
self.creatButton.frame = CGRectMake(0, kScreenH - 40 - height, kScreenW, 40);
}];
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
[UIView animateWithDuration:0.3 animations:^{
self.creatButton.frame = CGRectMake(0, kScreenH - 40, kScreenW, 40);
}];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: