2014-01-20 26 views
0

如何在屏幕上移动UITextField,使其不在iOS7中被键盘覆盖,然后在键盘被解除后将其返回到UIView内的原始位置?如何移动UITextField以响应iOS7中出现和消失的键盘?

+1

[如何使键盘存在时使UITextField向上移动]的可能重复(http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard- is-present) – katzenhut

+1

将其标记为重复。此外,你还问过之前谷歌这个? – katzenhut

+0

我需要UItextfield向上滚动当用户输入第一个字母在文本域 – user3182143

回答

1

在Viewcontroller.h

@interface ViewController : UIViewController<UITextFieldDelegate> 
{ 
CGFloat animatedDistance; 

} 
在ViewController.M

//粘贴任何地方低于4行上的类(的方法外)

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3; 
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2; 
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8; 
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216; 

在委托方法是

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    CGRect textFieldRect = 
[self.view.window convertRect:textField.bounds fromView:textField]; 
CGRect viewRect = 
[self.view.window convertRect:self.view.bounds fromView:self.view]; 

CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height; 
CGFloat numerator = 
midline - viewRect.origin.y 
- MINIMUM_SCROLL_FRACTION * viewRect.size.height; 
CGFloat denominator = 
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) 
* viewRect.size.height; 
CGFloat heightFraction = numerator/denominator; 
if (heightFraction < 0.0) 
{ 
    heightFraction = 0.0; 
} 
else if (heightFraction > 1.0) 
{ 
    heightFraction = 1.0; 
} 


animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); 

CGRect viewFrame = self.view.frame; 
viewFrame.origin.y -= animatedDistance; 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

[self.view setFrame:viewFrame]; 

[UIView commitAnimations]; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 

{ 
CGRect viewFrame = self.view.frame; 
viewFrame.origin.y += animatedDistance; 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

[self.view setFrame:viewFrame]; 

[UIView commitAnimations]; 

} 
+0

非常感谢,它适合你 –

+1

哇!一些比较古老的动画方法。鉴于问题标题问iOS 7,这是一个遗憾,你发布这一点 - 他们相当气馁。 – Luke

1

你必须赶上UIKeyboardWillShowNotification的通知,并编辑你的帧以将UITextField移动到正确的位置(例如通过使用[UIView animateWithDuration:(NSTimeInterval)动画:^(void)动画])。

这里是一个示例...

init,在UIKeyboardWillShowNotification添加一个观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil]; 

然后添加- keyboardWillShow方法来更新您的视图(假设_textfield是你UITextField的名称) :

- (void)keyboardWillShow { 
    NSLog(@"Show Keyboard"); 

    [UIView animateWithDuration:.2 animations:^{ 
     [_textField setFrame:_textField.frame]; 
     [_textField setFrame:CGRectMake(_textfield.frame.origin.x, _textfield.frame.origin.y - 240, _textfield.frame.size.width, _textfield.frame.size.height)]; 
    }]; 
} 

您可以使用UIKeyboardWillShowNotification通知n来执行反向操作。

0

我推荐使用UIScrollView将内容向上移动,当显示键盘时。 另请参阅Apple discourages us使用自定义常量的键盘大小:其他语言可能有不同的大小。正确的尺寸是嵌入在通知的用户信息:

获得通知键盘变化:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillBeShown:) 
              name:UIKeyboardWillShowNotification object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillBeHidden:) 
              name:UIKeyboardWillHideNotification object:nil]; 

记住清除观察者这样的:

-(void)dealloc { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

这些方法处理的键盘变化:

- (void)keyboardWillBeShown:(NSNotification*)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    CGRect kbRect = [self.view.window convertRect:[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] fromView:self.scrollView]; 
    CGSize kbSize = kbRect.size; 

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 
} 

- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    NSTimeInterval duration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    [UIView animateWithDuration:duration animations:^{ 
     UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
     self.scrollView.contentInset = contentInsets; 
     self.scrollView.scrollIndicatorInsets = contentInsets; 
    }]; 
} 

当然你也可以操作fra使用这些方法的UITextView的我。

+0

这是一个非常糟糕的复杂的方式来做到这一点。 – Gusutafu

+0

或者你也许回答了另外一个问题。使用scrollview移动_contents_很好,但是将一个输入字段放在滚动视图上以便移动它并不是一个好主意。只需使用自动布局,甚至改变其位置。 – Gusutafu

+0

正如我所说:“当然,你也可以用这些方法操作UITextView的框架。”只要内容大于显示键盘后留下的空间,我就使用滚动视图。这样您就可以滚动查看所有内容。 – NoilPaw