TextField输入时被Keyboard挡住的解决办法

我们在TextField输入时经常会出现被虚拟键盘挡住的现象,下面是具体的解决办法


120057456.png

120125331.png

 
 
  1. //要用到textfield的代理

  2. @interface ScrollerViewController ()<UITextFieldDelegate> {

  3.    BOOL keyboardIsShown;//键盘是否出现

  4.    CGPoint offset;//记录在键盘出现前,scrollview的contentOffset

  5. }

  6. @property (nonatomic, retain) UIScrollView *scrollView;

  7. @property (nonatomic, retain) UITextField *textField;

  8. @end

首先设置scrollview和加载textField


 
 
  1. - (void)viewDidLoad

  2. {

  3.    //设置ScrollView

  4.    scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];

  5.    self.scrollView.zoomScale = 1.0;

  6.    self.scrollView.contentSize = CGSizeMake(320, 1128);

  7.    self.scrollView.contentOffset = CGPointMake(0, 0);

  8.    self.scrollView.bounces = NO;

  9.    //设置显示textField

  10. for (int i = 0; i < 10; i++) {

  11.        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 20 + i * 38, 70, 30)];

  12.        label.text  = [NSString stringWithFormat:@"label%i",i + 1];

  13.        label.textAlignment = NSTextAlignmentCenter;

  14.        label.backgroundColor = [UIColor clearColor];

  15.        label.textColor = [UIColor whiteColor];

  16.        textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 20 + i * 38, 200, 30)];

  17.        self.textField.backgroundColor = [UIColor whiteColor];

  18.        self.textField.delegate = self;

  19.        self.textField.borderStyle = UITextBorderStyleRoundedRect;

  20.        [self.scrollView addSubview:label];

  21.        [self.scrollView addSubview:self.textField];

  22.        [label release];

  23.        [self.textField release];

  24.    }

  25.    [self.view addSubview:self.scrollView];

  26.    [super viewDidLoad];

  27.    // Do any additional setup after loading the view, typically from a nib.

  28. }

在View出现之前,注册两个通知。它能通知你什么时候keyboard出现或消失。


 
 
  1. - (void)viewWillAppear:(BOOL)animated{

  2.    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidShow:)

  3. name:UIKeyboardDidShowNotification

  4.                                                           object:self.view.window];

  5.    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidHide:)

  6. name:UIKeyboardDidHideNotification

  7.                                                          object:nil];

  8.    [super viewWillAppear:animated];

  9. }

当任何textField被点击时textFieldDidBeginEditing:被激活,你可以保存一个正在被点击textField的拷贝。


 
 
  1. - (void)textFieldDidBeginEditing:(UITextField *)textFieldView{

  2.    textField = textFieldView;

  3. }

当点击keyboard上的Return键时textFieldShouldReturn:方法会响应


 
 
  1. - (BOOL)textFieldShouldReturn:(UITextField *)textFieldView{

  2.    [textFieldView resignFirstResponder];

  3. returnNO;

  4. }

当keyboard隐藏,结束输入时textFieldDidEndEditing:方法会被textfield会被设为nil。


 
 
  1. - (void)textFieldDidEndEditing:(UITextField *)textFieldView{

  2.    textField = nil;

  3. }

当键盘出现时,通过notification设置,keyboardDidShow:方法开始响应。方法中最重要的是获取键盘的高度,例如输入中文时,键盘会多出一行选字提示,高度就会发生变化,所以不能设成固定的216。然后重新设置ScrollView的frame,将textField滚动到可见位置。

如果键盘已经出现时,点击另一个textField,这个方法会自动退出。因为keyboardIsShown已经设为YES。如果点击的textField有部分隐藏,它会自动滚动到可见区域。


 
 
  1. - (void)keyboardDidShow:(NSNotification *)notification{

  2.    //当键盘已经出现

  3.    if (keyboardIsShown) {

  4. return;

  5.    }

  6.    NSDictionary *info = [notification userInfo];

  7.    //获取keyboard的size

  8.    NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

  9.    CGRect keyboardRect = [self.view convertRect:[aValue CGRectValue] fromView:nil];

  10.    NSLog(@"%f", keyboardRect.size.height);

  11.    //获取scrollView的contentOffset,当前的滚动位置

  12.    offset = self.scrollView.contentOffset;

  13.    //当键盘存在时,重新设置scrollView的size

  14.    CGRect viewFrame = [self.scrollView frame];

  15.    viewFrame.size.height -= keyboardRect.size.height;

  16.    self.scrollView.frame = viewFrame;

  17.    //将textField滚动到可见区域

  18.    CGRect textFieldRect = [self.textField frame];

  19.    [self.scrollView scrollRectToVisible:textFieldRect animated:YES];

  20.    keyboardIsShown = YES;

  21. }

当键盘消失时,keyboardDidHide:开始响应。还原ScrollView的frame


 
 
  1. - (void)keyboardDidHide:(NSNotification *)notification{

  2.    //获取键盘size

  3.    NSDictionary* info = [notification userInfo];

  4.    NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

  5.    CGRect keyboardRect = [self.view convertRect:[aValue CGRectValue] fromView:nil];

  6.    //重新设置scrollView的size,使它还原到初始size

  7.    CGRect viewFrame = [self.scrollView frame];

  8.    viewFrame.size.height += keyboardRect.size.height;

  9.    self.scrollView.frame = viewFrame;

  10.    //还原滚动位置

  11.    self.scrollView.contentOffset = offset;

  12.    keyboardIsShown =   NO;

  13. }

最后在View消失前,移除两个通知


 
 
  1. - (void)viewWillDisappear:(BOOL)animated{

  2.    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];

  3.    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];

  4. }


你可能感兴趣的:(ios,textfield,keyboard)