iOS键盘遮挡输入框,输入区域自动上移

在iOS开发过程当中,遇到关于键盘遮挡输入框的问题,经过网络参考与实践,总结如下:

登录窗口,上下放置两个UITextField,一个用户名,一个密码,放置的在屏幕下方1/3处,当点击用户名时,自动弹出键盘,正好挡住了输入框

解决思路:

1、BLoginViewController 实现UITextViewDelegate的方法

 1  //实现了UITextFieldDelegate中的方法,当对TextField进行编辑即键盘弹出时,自动将输入框上移
 2  -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
 3      NSTimeInterval animationDuration=0.30f;
 4      [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
 5      [UIView setAnimationDuration:animationDuration];
 6      float width = self.view.frame.size.width;
 7      float height = self.view.frame.size.height;
 8      //上移n个单位,按实际情况设置
 9      CGRect rect=CGRectMake(0.0f,-130,width,height);
10      self.view.frame=rect;
11      [UIView commitAnimations];
12      return YES;
13  }

2、为输入框设置代理

 1 - (void)viewDidLoad
 2 {
 3      [super viewDidLoad];
 4      
 5      //状态栏白色字体
 6      [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
 7      
 8      UIColor *btnBgColor = [UIColor colorWithWhite:1.0f alpha:1.0];
 9      [_buttonLogin setBackgroundColor:btnBgColor];
10      
11      //为输入框添加代理
12      _textFieldUserName.delegate = self;
13      _textFieldPassword.delegate = self;
14 
15 }

 效果:

 

 参考:http://blog.csdn.net/ryantang03/article/details/8203605

原文地址:https://www.cnblogs.com/blacksonny/p/3932820.html