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

iOS相应键盘高度变化,相应控件随之变化的代码段

2013-03-29 15:14 357 查看
//注册通知
[[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 notification

- (void)keyboardWillShow:(NSNotification *) notification {
float animationDuration = [[[notification
userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey]
floatValue];
CGFloat height = [[[notification
userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]
CGRectValue].size.height;

CGRect bottomBarFrame =
self.mToolBar.frame;
{
[UIView
beginAnimations:@"bottomBarUp"
context:nil];
[UIView setAnimationDuration: animationDuration];
[UIView
setAnimationCurve:UIViewAnimationCurveEaseInOut];
bottomBarFrame.origin.y =
self.view.bounds.size.height -
44 - height;
self.mToolBar.frame = bottomBarFrame;
[UIView
commitAnimations];
}
}

- (void)keyboardWillHide:(NSNotification *) notification {
float animationDuration = [[[notification
userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey]
floatValue];
CGFloat height = [[[notification
userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]
CGRectValue].size.height;

CGRect bottomBarFrame =
self.mToolBar.frame;
if (bottomBarFrame.origin.y <
300)
{
[UIView
beginAnimations:@"bottomBarDown"
context:nil];
[UIView setAnimationDuration: animationDuration];
[UIView
setAnimationCurve:UIViewAnimationCurveEaseInOut];
bottomBarFrame.origin.y += height;
self.mToolBar.frame = bottomBarFrame;
[UIView
commitAnimations];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: