使视图向上滑动以便为键盘腾出空间?

发布于 2024-11-05 07:43:19 字数 77 浏览 0 评论 0原文

我刚刚开始学习 iPhone 应用程序编程,我似乎不知道如何在键盘出现时让视图滑开(这样你仍然可以看到正在输入的文本字段)。它是如何完成的?

I just started learning to program iPhone apps and I can't seem to figure out how to make the view slide out of the way when the keyboard appears (so you can still see the text field you're typing in). How is it done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

吾性傲以野 2024-11-12 07:43:19

如果视觉上没问题,最简单的方法是移动整个 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];  
}

If it is OK visually, the easiest is to move the entire self.view.frame and then move it back down when finished.

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];  
}
美男兮 2024-11-12 07:43:19

一种方法是将所有内容包含在 UIScrollView 中,然后向上滚动内容。另一种方法是自己移动视图,通常在核心动画的帮助下使其看起来更漂亮。

一个好的起点是文档。甚至还有一个部分标记为移动位于键盘下方的内容,这将指向你的方向是正确的。

One way to do it is to contain everything in a UIScrollView and then scroll the contents upward. Another way is to move the view yourself, usually with the help of Core Animation to make it look nice.

A good place to start is with the documenation. There's even a section helpfully labelled Moving Content That Is Located Under the Keyboard which will point you in the right direction.

日暮斜阳 2024-11-12 07:43:19

假设您需要在标签为 4 的文本字段上向上移动视图(当您有超过 1 个 txt 字段并且其中之一被键盘覆盖时),然后使用 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 相同的代码

。向下移动,您需要在导航栏中添加一个按钮,或者添加一个按钮。在工具栏中,并通过相同的动画将该工具栏设置在键盘上。对于按钮,您需要相同的代码来向下移动,这适用于文本字段。

希望这对您有帮助。

suppose you need to move up view on a text field which tag is 4(when you have more than 1 txt fields and one of them covered by the keyboard) then use textField delegate method

- (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];
    }

}

this moves your view. now for moving down you need code in textField anothe delegate method

- (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];
    }
}
}

In case of textview you need to a button and for moving up your view you need this delegate

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView

and use same code as textField

And moving down you need a button either in navigation bar or add in toolbar and set that tool bar over the keyboard by same animation.For button you need same code for moving down which is applicable for textField.

Hope this helps you.

与往事干杯 2024-11-12 07:43:19

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

I found that using the keyboard notifications worked better for my application than using the UITextField delegate protocol of textFieldDidBeginEditing and textFieldDidEndEditing. The notifications are keyboardWillShow and keyboardWillHide. One can test for a UITextField or UITextView that would require the view to move withing these notifications and then conditionally move the view. The advantage in my application is that I have many UITextTields and the notifications make it easier to keep the view in place above the keyboard when editing moves from one field to the other.

横笛休吹塞上声 2024-11-12 07:43:19
  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];

}

  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];

}

太阳公公是暖光 2024-11-12 07:43:19

使用 iOS 15 或更高版本时,您可以使用 UIViewkeyboardLayoutGuide 属性在键盘出现时自动调整视图底部的大小。

自动布局约束示例,将 myView 置于屏幕键盘顶部:

NSLayoutConstraint.activate([
    myView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
    myView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
    myView.topAnchor.constraint(equalTo: view.topAnchor),
    myView.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor)
])

更多信息和示例代码可在此处找到:https://developer.apple.com/documentation/uikit/keyboards_and_input/ adjustment_your_layout_with_keyboard_layout_guide

When using iOS 15 or newer, you can make use of the keyboardLayoutGuide property of the UIView to automatically resize the bottom of a view, when the keyboard appears.

Example auto layout constraint, to have myView on top of the onscreen keyboard:

NSLayoutConstraint.activate([
    myView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
    myView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
    myView.topAnchor.constraint(equalTo: view.topAnchor),
    myView.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor)
])

More info and example code can be found here: https://developer.apple.com/documentation/uikit/keyboards_and_input/adjusting_your_layout_with_keyboard_layout_guide

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文