我们在开发注册界面的时候,最后几个注册条件常常容易被系统弹出的键盘遮挡,如下图:
可以看见,邮箱条件被遮挡掉了,怎么解决呢?我是通过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);
}];
}
结果图: