iPhone - 当键盘出现时如何滚动 UITextField 并调整 UITextView 的大小

发布于 2024-10-14 10:41:22 字数 2009 浏览 4 评论 0原文

我有一个 UIScrollView,其中有一个 UITextField 和一个 UITextView。 我想防止键盘隐藏我们正在处理的字段。 如果我的用户点击文本字段,我希望它滚动到屏幕顶部。 如果我的用户点击 TextView,我希望它调整大小到键盘顶部左侧的空白位置。 我的键盘有一个附件视图(工具栏)。

现在,我捕获了 UIKeyboardWillShowNotification 通知,并使用了这段代码,但它实际上不起作用(文本视图已调整大小,但仍在键盘后面): 当我没有 UIScrollView 时,这段代码运行良好。

- (void)keyboardWillShow:(NSNotification *)notification {

    if ([self.noteView isFirstResponder] == NO) return;

    /*
     Reduce the size of the text view so that it's not obscured by the keyboard.
     Animate the resize so that it's in sync with the appearance of the keyboard.
     */

    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];                            // Get the origin of the keyboard when it's displayed.
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];   // Get the duration of the animation.

    // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

    CGFloat keyboardTop = keyboardRect.origin.y;
    CGRect newTextViewFrame = self.view.bounds;
    newTextViewFrame.size.height = keyboardTop - self.view.frame.origin.y; // Using bounds here does not help

    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];

    // Animate the resize of the text view's frame in sync with the keyboard's appearance.
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];

     self.noteView.frame = newTextViewFrame;

    [UIView commitAnimations];
}

那么你能帮我:

  • 使这个适用于文本视图吗?

  • 让Textview回到原来的位置?

  • 如何在没有任何调整大小功能的情况下滚动 TextField?

谢谢

I have a UIScrollView inside which I have a UITextField and a UITextView.
I want to prevent the keyboard to hide the field we are working on.
If my user tap on the TextField, I want it to scroll to top of the screen.
If my user tap on the TextView, I want it to resize to the empty place left on the top of the keyboard.
My keyboard has an accessory view (a toolbar).

For now, I catch the UIKeyboardWillShowNotification notification, and I use this code, that does not really work (the textview is resized, but is still behind the keyboard) :
This code worked well when I hadn't the UIScrollView.

- (void)keyboardWillShow:(NSNotification *)notification {

    if ([self.noteView isFirstResponder] == NO) return;

    /*
     Reduce the size of the text view so that it's not obscured by the keyboard.
     Animate the resize so that it's in sync with the appearance of the keyboard.
     */

    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];                            // Get the origin of the keyboard when it's displayed.
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];   // Get the duration of the animation.

    // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

    CGFloat keyboardTop = keyboardRect.origin.y;
    CGRect newTextViewFrame = self.view.bounds;
    newTextViewFrame.size.height = keyboardTop - self.view.frame.origin.y; // Using bounds here does not help

    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];

    // Animate the resize of the text view's frame in sync with the keyboard's appearance.
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];

     self.noteView.frame = newTextViewFrame;

    [UIView commitAnimations];
}

So could you help me to :

  • make this work for the textview ?

  • make the Textview go back to its original position ?

  • How may I do to scroll the TextField without any resizing feature ?

Thank you

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

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

发布评论

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

评论(1

瞄了个咪的 2024-10-21 10:41:22

您可以使用以下方式以编程方式滚动 uiscrollview:

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated

不确定这是否适用于您的应用程序。

Could you just programmatically scroll the uiscrollview using this:

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated

Not sure if this will work for your application or not.

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