UITextField 弹出键盘时遮盖住textField的问题

       在开发中,遇到 textField输入框 靠屏幕下方, 或者 屏幕下方有一些button 时, 弹出键盘就会把这些控件遮盖住, 只要我们在弹出键盘时 改变self.view 的frame属性,就可以实现视图的上移下移问题.


在开始编辑时触发textField的代理方法(需要遵守textField的协议)

- (void)textFieldDidBeginEditing:(UITextField *)textField
{   
   [self changeHeight:230];  // 原来键盘高度216, 但是在我的模拟器上要比216长, 所以 自己定义了
}

return键收回键盘时 触发的方法 主要是靠[self recoverFrame] 来恢复frame的

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//  NSString *str = textField.text;
 // 利用str 来传值//    NSLog(@"%@", str);
 [textField resignFirstResponder];
 [self recoverFrame];
 textField.text = nil;
 return YES;
}

恢复view的frame方法

- (void)recoverFrame
{
 self.webView.userInteractionEnabled = 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];
}

利用动画效果实现frame的改变


- (void)changeHeight:(CGFloat)keyBoardHeight
{
    CGRect frame = self.textFD.frame;
    float offset = frame.origin.y + 32 - 
(self.view.frame.size.height - keyBoardHeight);
    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];
}

还有一个问题 就是当输入文字的时候会有文字提示,这个长条也会遮挡住textField, 只要在下面的方法中再次调用改变高度的方法就可以了.

// 当点击键盘弹出文字提示时
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange
:(NSRange)range replacementString:(NSString *)string
{
    [self changeHeight:265];// 自己定义一下新的高度
    return YES;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值