iOS开发 编辑框被系统弹出的软键盘遮挡问题
我们在开发注冊界面的时候,最后几个注冊条件经常easy被系统弹出的键盘遮挡,例如以下图:
能够看见,邮箱条件被遮挡掉了,怎么解决呢?我是通过UITextField的代理加计算偏移量:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.emailTextField.delegate = self;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
// 1.取得被遮挡的邮箱的textField的frame
CGRect frame = self.emailTextField.frame;
// 2.计算被遮挡的textField的bottom代表的高度(Y坐标) 与 能不被键盘遮挡的最大高度(Y坐标) 的距离
int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216);
[UIView animateWithDuration:0.3 animations:^{
if (offset > 0) {
// 让整个view向上偏移差距
self.view.frame = CGRectMake(0, -offset, self.view.frame.size.width, self.view.frame.size.height);
}
}];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
[UIView animateWithDuration:0.3 animations:^{
// 编辑结束后重置为原来的
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}];
}
结果图:

浙公网安备 33010602011771号