您的位置:首页 > 产品设计 > UI/UE

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

2015-10-16 19:34 651 查看
在开发中,遇到 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: