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

iOS 监听键盘弹出的高度,避免挡住界面

2015-06-11 18:41 423 查看
iPhone 的键盘 和 ipod 键盘高度完全不同,要注意

不废话 直接代码走起。

- (void)viewDidLoad {
[super viewDidLoad];

//    监听键盘通知
[[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 *)showNot{
//    1.取出键盘frame
CGRect keyboardFrame= [showNot.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat upRectFrame = keyboardFrame.size.height /2;

//    2.键盘弹出的时间
CGFloat duration=[showNot.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//    3.执行动画
[UIView animateWithDuration:duration animations:^{
//        self.LoginView.transform=CGAffineTransformMakeTranslation(0,-keyboardFrame.size.height);
self.view.transform=CGAffineTransformMakeTranslation(0,-upRectFrame);
}];

}

-(void)keyboardWillHide:(NSNotification *)hideNot{
//    2.键盘弹出的时间
CGFloat duration=[hideNot.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//    3.执行动画
[UIView animateWithDuration:duration animations:^{
self.view.transform=CGAffineTransformIdentity;
}];
}

//释放通知
-(void)delete:(id)sender{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: