遵循textfield协议在输入开始时和输入结束两个方法里边改变当前视图的frame就可以了!
//iOS TextField输入框点击键盘时随着键盘上移
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
int offset = self.view.frame.origin.y - 216.0;//iPhone键盘高
[UIView animateWithDuration:0.5 animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, -offset);
}];
}
//输入框编辑完成以后,将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.5 animations:^{
self.view.transform = CGAffineTransformIdentity;
}];
}
pod ‘IQKeyboardManager’
把这个install到工程中,一般情况的输入问题都不用考虑了。