将视图向上滑动以腾出键盘空间?

4

我刚开始学习编程iPhone应用程序,但似乎无法弄清如何在键盘出现时使视图滑动到一边(这样您仍然可以看到正在输入的文本字段)。这是如何完成的?


2
Michael Tyson编写了一个组件,可以自动化此过程:一种可直接使用的通用解决方案,将文本字段移出键盘的路径 - user557219
那个链接真的非常好用!谢谢! - BlackHatSamurai
5个回答

2

如果视觉效果良好,最简单的方法是移动整个self.view.frame,然后在完成后将其移回原位。

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3f;

- (void) animateForToNewYPosition:(int)newYPosition {
    // move for kdb
    if (self.view.frame.origin.y == newYPosition) {
        return;
    }

    // start animation
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];  

    // move it
    self.view.frame.origin.y = newYPosition;

    [UIView commitAnimations];  
}

你怎么称呼这个方法? - kendotwill
使用UITextField代理,您可以将调用animateForToNewYPosition:的方法放在委托方法textFieldDidBeginEditing:中。一个好的例子/答案在这里:http://stackoverflow.com/a/17887070/203960 - dredful

1

一个方法是将所有内容包含在UIScrollView中,然后向上滚动内容。另一种方法是自己移动视图,通常借助Core Animation使其看起来不错。

开始的好地方是文档。甚至有一个帮助标签为移动位于键盘下方的内容的部分,它将指导您朝正确的方向前进。


好的,我该如何使用UIScrollView实现它? - Ry-
上面添加了一个链接,告诉你需要知道的内容。 - Caleb

0
我发现使用键盘通知比使用UITextField委托协议的textFieldDidBeginEditing和textFieldDidEndEditing更适合我的应用程序。这些通知是keyboardWillShow和keyboardWillHide。可以测试需要在这些通知中移动视图的UITextField或UITextView,然后有条件地移动视图。在我的应用程序中的优点是我有许多UITextTields,通知使得在编辑从一个字段移动到另一个字段时更容易保持视图在键盘上方的位置。

0
  http://objectivecwithsuraj.blogspot.in/2012/06/making-view-slide-up-to-make-room-for.html

  Add a UIScrollview - scrollview to your UIView and set delegates for UITextFields & 
     UIScrollview

  - (BOOL)textFieldShouldReturn:(UITextField *)textField 
  {
     if (textField == txtFieldName)
   {
         [txtFieldCellNo becomeFirstResponder];
   }
   else if (textField == txtFieldCellNo)
   {
         [txtFieldEmail becomeFirstResponder];
   }
   else
  {
       [textField resignFirstResponder];

  }
return YES;
}

 - (void)textFieldDidBeginEditing:(UITextField *)textField
 {
    [self animateTextField:txtFieldName up:YES];
 }


- (void)textFieldDidEndEditing:(UITextField *)textField
{
   [self animateTextField:txtFieldEmail up:NO];
}


- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    const int movementDistance = 80; 
    const float movementDuration = 0.3f;
    int movement = (up ? -movementDistance : movementDistance);
    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame,0, movement);
    [UIView commitAnimations];

}


0
假设您需要将标记为4的文本字段上移(当您有多个文本字段并且其中一个被键盘覆盖时),那么请使用textField委托方法。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField   
{
if(textField.tag==4)
    CGRect viewFrame;
    viewFrame=self.view.frame;
    if(viewFrame.origin.y==-100)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:.3];
        viewFrame.origin.y+=100;
        self.view.frame=viewFrame;
        [UIView commitAnimations];
    }

}

这会移动你的视图。现在,如果要向下移动,你需要在textField中编写另一个委托方法的代码。

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
   if(textField.tag==4)
{
 CGRect viewFrame;
    viewFrame=self.view.frame;
    if(viewFrame.origin.y==-100)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:.3];
        viewFrame.origin.y+=100;
        self.view.frame=viewFrame;
        [UIView commitAnimations];
    }
}
}

针对TextView,您需要一个按钮,并且为了向上移动您的视图,您需要该委托。

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView

使用与textField相同的代码

向下移动时,您需要一个按钮,可以放在导航栏中或添加到工具栏中,并通过相同的动画将该工具栏设置在键盘上方。对于按钮,您需要适用于textField的相同移动代码。

希望这可以帮助您。


我把这个放在 ViewController 文件里,对吧?它应该自动调用的,是吗?但是它对我不起作用... - Ry-
你需要将textField的代理与文件所有者连接,或者通过编程方式设置yourTxtField.delegate=self;然后这些代理方法将被自动调用。 - Ishu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接