美文网首页
解决软键盘遮挡输入框方法

解决软键盘遮挡输入框方法

作者: 罂粟之城 | 来源:发表于2019-04-25 23:16 被阅读0次

键盘遮挡输入框问题的出现,是源于当我们在界面中使用 UITextFieldUITextView,UISearchBar 或者其它可以调出键盘的功能。在使用虚拟键盘输入时出现遮挡输入框或者遮挡关键视图时,就会产生我们需要解决的问题。

可以使用下面的几种方法间接实现效果:

  • 1.将视图上的所有的东西都添加到一个滚动视图对象( UIScrollView )中,然后滚动视图可以上下滑动实现输入框不被软键盘覆盖。注:此方法注意在调出软键盘时,可能需从设 UIScrollView的 contentSize .

  • 2.通过一个通知 UIKeyboardDidShowNotification 去实现的,需要用到事件监听,而且需要自己定义并实现 将要开始编辑结束编辑 这两个监听事件中的方法.

  • 3.使用 调出键盘 和 关闭键盘 的时候视图位置偏移移动

1).不使用上述的方法的第一种解决方法,代码如下:
   //开始编辑输入框的时候,软键盘出现,执行此事件 
    -(void)textFieldDidBeginEditing:(UITextField *)textField 
    { 
        CGRect frame = textField.frame; 
          //改进方法:CGRect frame = [textField convertRect:textField.frame toView:self.view];
               否则的话对于textField是嵌套在别的view中的情况,self.view上移幅度不够
        int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0-35.0);
            //键盘高度216,键盘上的bar高度为35;textField高度32,如果这个控件位于另一个视图上还需要加上父视图的Y值,才可以是需要的偏移量。          
        NSTimeInterval animationDuration = 0.30f; 
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
        [UIView setAnimationDuration:animationDuration];          
            //将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示 
        if(offset > 0) 
          self.view.frame = 
            CGRectMake(0.0f, -offset, self.view.frame.size.width,self.view.frame.size.height);          
        [UIView commitAnimations]; 
    }      
   //当用户按下return键或者按回车键,keyboard消失 
    -(BOOL)textFieldShouldReturn:(UITextField *)textField 
    { 
        [textField resignFirstResponder]; 
        return YES; 
    }      
    //输入框编辑完成以后,将视图恢复到原始状态 
    -(void)textFieldDidEndEditing:(UITextField *)textField 
    { 
        self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); 
    }  
    
2)不使用上述的方法的第二种解决方法,代码如下:
   !!!! a. 首先要写上UITextFieldDelegate代理协议
         b. 添加文本输入框,申明两个属性:
                @property (nonatomic, weak) UITextField * userNameText;
                @property (nonatomic, weak) UITextField * userPwdText;
        c.
                UITextField * userNameText = [AutolayoutView autolayoutTextFieldWithPlaceholder:@"账号"];
                userNameText.delegate = self;
                [self.view addSubview:userNameText];
                self.userNameText = userNameText;
        UITextField * userPwdText = [AutolayoutViewautolayoutTextFieldWithPlaceholder:@"密码"];
                userPwdText.delegate = self;
                [self.view addSubview:userPwdText];
                self.userPwdText = userPwdText;
         d. 实现代理方法
            此处主要解决:
    // 当前点击textfield的坐标的Y值 + 当前点击textFiled的高度 - (屏幕高度- 键盘高度 - 键盘上tabbar高度)
    // 在这一步就是这个当前textfiled的的最大Y值和键盘的最全高度的差值,用来计算整个view的偏移量
        - (void)textFieldDidBeginEditing:(UITextField *)textField
            {
                NSLog(@"textFieldDidBeginEditing");
                CGRect frame = textField.frame;
                CGFloat heights = self.view.frame.size.height;
    // 当前点击textfield的坐标的Y值 + 当前点击textFiled的高度 - (屏幕高度- 键盘高度 - 键盘上tabbar高度)
      // 在这一部 就是了一个 当前textfile的的最大Y值 和 键盘的最全高度的差值,用来计算整个view的偏移量
                    int offset = frame.origin.y + 42- ( heights - 216.0-35.0);//键盘高度216
                    NSTimeInterval animationDuration = 0.30f;
                    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
                    [UIView setAnimationDuration:animationDuration];
                    float width = self.view.frame.size.width;
                    float height = self.view.frame.size.height;
                    if(offset > 0)
                        {
                            CGRect rect = CGRectMake(0.0f, -offset,width,height);
                            self.view.frame = rect;
                        }
                    [UIView commitAnimations];
                }
            e. 点击空白处的时候让其回到原来位置
                /**
                    *  textField 取消选中状态
                */
                -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
                {
                    NSLog(@"touchesBegan");
                    [self.view endEditing:YES];
                    NSTimeInterval animationDuration = 0.30f;
                    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
                    [UIView setAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
                self.view.frame = rect;
                [UIView commitAnimations];
                }
            f.还有点击键盘的return键的时候恢复原状就要在
                - (BOOL)textFieldShouldReturn:(UITextField *)textField;里头处理。
                切记一定要判断当前的textFiled是否是你点击的self.userNameText了。在让他恢复原状。

相关文章

网友评论

      本文标题:解决软键盘遮挡输入框方法

      本文链接:https://www.haomeiwen.com/subject/fmxxnqtx.html