gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:16:22 27 4
gpt4 key购买 nike

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

现在,我捕获了 UIKeyboardWillShowNotification 通知,并且使用了这段代码,但它实际上不起作用( TextView 已调整大小,但仍然位于键盘后面):当我没有 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 ?

  • 让 Textview 返回到原来的位置?

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

谢谢

最佳答案

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

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

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

关于iPhone - 当键盘出现时如何滚动 UITextField 并调整 UITextView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4800345/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com