1.bug描述: 一,系统键盘,当键盘弹起后,再执行另外的alert操作,键盘收起,界面Y值并不会复原;二,第三方键盘,键盘弹出,界面不会往上移动,而是会往下移动;
2.bug解决:
//然后,根据键盘高度将当前视图向上滚动同样高度。
-(void)keyboardWillAppear:(NSNotification *)notification
{
if ([self.bankAlert.userNameField isFirstResponder] || [self.bankAlert.IDcardField isFirstResponder] || [self.bankAlert.phoneNumField isFirstResponder]) {
if (self.isMove) {
return;
}
self.isMove = YES;
self.change = [self keyboardEndingFrameHeight:[notification userInfo]];
CGRect currentFrame = self.contentView.frame;
// currentFrame.origin.y = currentFrame.origin.y - self.change + 130;
// currentFrame.origin.y = currentFrame.origin.y - 86;
currentFrame.origin.y = -22;
self.contentView.frame = currentFrame;
}
}
//最后,当键盘消失后,视图需要恢复原状。
-(void)keyboardWillDisappear:(NSNotification *)notification
{
if ([self.bankAlert.userNameField isFirstResponder] || [self.bankAlert.IDcardField isFirstResponder] || [self.bankAlert.phoneNumField isFirstResponder]) {
CGRect currentFrame = self.contentView.frame;
// currentFrame.origin.y = currentFrame.origin.y + self.change - 130; 第三方键盘bug,改用下面一种
// currentFrame.origin.y = currentFrame.origin.y + 86; alert弹框bug,改用以下位移高度
currentFrame.origin.y = 64; //最终使用固定高度
self.contentView.frame = currentFrame;
self.isMove = NO;
}
}
//其次键盘的高度计算:
-(CGFloat)keyboardEndingFrameHeight:(NSDictionary *)userInfo//计算键盘的高度
{
CGRect keyboardEndingUncorrectedFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
CGRect keyboardEndingFrame = [self.contentView convertRect:keyboardEndingUncorrectedFrame fromView:nil];
return keyboardEndingFrame.size.height;
}
3.注意点:1.不同键盘类型高度不一样,如果要求上移位置一致,需要给定高度;
2.第三方键盘可以监听弹出和收起,但是无法通过系统方法直接拿到高度;
3.alert操作会endedit,但是不会恢复UI位置;