gpt4 book ai didi

ios - 通过调整工具栏中包含的 UITextView 的大小来调整 UIToolbar 的大小

转载 作者:行者123 更新时间:2023-11-29 01:55:39 25 4
gpt4 key购买 nike

我有一个 UIToolbar,它位于我的 ViewController 底部,并随着键盘向上滑动而移动到键盘顶部并带有动画。换句话说,它随键盘移动。现在,我正在为工具栏中包含的 UITextView 创建一种方法,以便在用户在 textView 中键入换行符时调整其高度。到目前为止,我在类里面使用这段代码:

- (void)textViewDidChange:(UITextView *)textView
{
CGFloat fixedWidth = textView.frame.size.width;
CGFloat fixedWidthToolbar = self.navigationController.toolbar.frame.size.width;

CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGSize newSizeToolbar = [self.navigationController.toolbar sizeThatFits:CGSizeMake(fixedWidthToolbar, MAXFLOAT)];

CGRect newFrame = textView.frame;
CGRect newToolbarFrame = self.navigationController.toolbar.frame;

newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
newToolbarFrame.size = CGSizeMake(fmaxf(newSizeToolbar.width, fixedWidthToolbar), newSizeToolbar.height);
textView.frame = newFrame;
self.navigationController.toolbar.frame = newToolbarFrame;
}

除了工具栏,一切正常。通过工作,我的意思是 UITextView 会在添加换行符时增长(用户输入大量文本)。这里的问题是我没有看到工具栏像 textView 那样变大。我相信我的问题是我使用了与调整 TextView 大小相同的代码来调整工具栏的大小。

编辑:这里的一般想法是允许 UITextView 和 UIToolbar 同时调整大小,允许用户看到正在 UITextView 中写入的文本。

我可以做些什么来实现这个目标?我做错了什么?

最佳答案

您必须计算您的 textView 中的行数并为每个更改/行增量触发 frameUpdate,这已被讨论 here

然后你需要同时更新框架并根据变化再次计算它..

关于行更新,您的代码可能如下所示:

NSInteger linesPrevious = 0;//global variable to monitor changes

- (void)textViewDidChange:(UITextView *)textView
{
[self checkNumberOfLines:YourTextView.text];
}

- (void)checkNumberOfLines:(UITextView *)textView
{
//compute the number of lines
NSInteger linesNew = textView.contentSize.height/textView.font.lineHeight;

if (linesNew != linesPrevious)
{
// make updates here
}
}

我建议您首先根据当前的 contentSize 计算 textView.frame,然后计算 UIToolbar.Frame

并且不要忘记更新 .origin.y 或您的工具栏以将其保持在正确的位置,假设您的工具栏高度为 44px,您需要减去 .origin.y 的超出部分,例如:

frame = YourToolBar.frame;
// example you updated the height with `64px`
frame.size.height = 64;

// you also need to update the `.origin.y`
frame.origin.y -= (frame.size.height/*new*/ - YourToolBar.frame.size.height/*current*/);

此外,如果您正在使用 NSNotificationCenter 来更新您的工具栏的框架。您需要将原始框架存储在 keyboardHideEvent 的全局变量中,以便您能够轻松地将框架恢复到其原始位置..


希望对你有帮助,干杯! ;)

关于ios - 通过调整工具栏中包含的 UITextView 的大小来调整 UIToolbar 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30903998/

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