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];
}