在iOS开发过程当中,遇到关于键盘遮挡输入框的问题,经过网络参考与实践,总结如下:
登录窗口,上下放置两个UITextField,一个用户名,一个密码,放置的在屏幕下方1/3处,当点击用户名时,自动弹出键盘,正好挡住了输入框
解决思路:
1、BLoginViewController 实现UITextViewDelegate的方法
1 //实现了UITextFieldDelegate中的方法,当对TextField进行编辑即键盘弹出时,自动将输入框上移 2 -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ 3NSTimeInterval animationDuration=0.30f; 4[UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 5[UIView setAnimationDuration:animationDuration]; 6float width = self.view.frame.size.width; 7float height = self.view.frame.size.height; 8//上移n个单位,按实际情况设置 9CGRect rect=CGRectMake(0.0f,-130,width,height);10self.view.frame=rect;11[UIView commitAnimations];12return YES;13 }
2、为输入框设置代理
1 - (void)viewDidLoad 2 { 3[super viewDidLoad]; 4 5//状态栏白色字体 6[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; 7 8UIColor *btnBgColor = [UIColor colorWithWhite:1.0f alpha:1.0]; 9[_buttonLogin setBackgroundColor:btnBgColor];1011//为输入框添加代理12_textFieldUserName.delegate = self;13_textFieldPassword.delegate = self;14 15 }
效果:
参考:/ryantang03/article/details/8203605
如果觉得《iOS键盘遮挡输入框 输入区域自动上移》对你有帮助,请点赞、收藏,并留下你的观点哦!