简单地调用UITextView`sizeThatFits:`会导致毛刺滚动/输入行为? [英] Simply calling UITextView `sizeThatFits:` causes glitchy scrolling / input behavior?

查看:133
本文介绍了简单地调用UITextView`sizeThatFits:`会导致毛刺滚动/输入行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在iOS 8中使用 UITextView sizeThatFits:会导致毛刺滚动行为。文本视图不断滚动远离您键入的行。它似乎滚动到视图的顶部然后再返回。

I find that in iOS 8 using UITextView sizeThatFits: causes glitchy scrolling behavior. The Text View is constantly scrolling away from the line you are typing on. It seems to be scrolling to the top of the view and then back again.

如果重要,视图设置为 inputAccessoryView

If it matters, the view is set as an inputAccessoryView.

通过键盘我输入: 1返回2返回3返回4

TextView 我输入 4 之前的那一刻:

The TextView the moment before I type 4:

在委托方法中,我调用 sizeThatFits:

In the delegate method I call sizeThatFits:.

- (void)textViewDidChange:(UITextView *)textView {
    [textView sizeThatFits:CGSizeMake(100, 100)];
}

TextView 向上滚动到顶部。输入发生在视图下方。当您键入时,抖动,毛刺的滚动运动到顶部然后回到您的线。输入发生在键盘下方。非常讨厌。

TextView scrolls up to the top. Input happens below the view. Jittery, glitchy scrolling movement up to the top and then back to your line as you type. Input occurs under the keyboard. Extremely annoying.

如果我注释掉该行:

//[textView sizeThatFits:CGSizeMake(100, 100)];

现在当我输入 4 时我们很好,在最后一行顺利输入:

Now when I type 4 we have nice, smooth typing on the last line:

UIScrollView sizeThatFits: docs state:

The UIScrollView sizeThatFits: docs state:

此方法不会调整接收器的大小。

This method does not resize the receiver.

所以我很困惑为什么这会有任何对文本字段的滚动/输入的影响。

So I'm confused why this would have any effect on the scrolling/input of the textfield.

有没有办法避免这种毛刺滚动?

Is there any way to avoid this glitchy scrolling?

如何在没有遇到此错误的情况下计算文本视图的适合高度?

How can you calculate the "height that fits" for a Text View without hitting this bug?

推荐答案

我遇到了完全相同的问题我花了5个小时来解决这个令人讨厌的苹果虫,我希望我能寄给他们一张发票!
我最终做的是创建原始UItextView的副本:

I had the exact same problem and it took me 5 hours to solve this nasty apple bug, I wish I could send them an invoice! What I end up doing was creating a copy of my original UItextView:

self.textViewCopy = [[UITextView alloc] initWithFrame:self.textView.frame];
[self.textViewCopy setFont:self.textView.font];

并且不要将其添加为子视图。

And don't add it as a subview.

然后在副本上调用sizeThatFits(这将搞砸我们不关心的副本并获取我们需要的信息):

Then instead call the sizeThatFits on the copy (which will screw up the copy which we don't care about and gets us the information we need):

[self.textViewCopy setText:self.textView.text];
CGSize size = [self.textViewCopy sizeThatFits:CGSizeMake(fixedWidth, CGFLOAT_MAX)];

这篇关于简单地调用UITextView`sizeThatFits:`会导致毛刺滚动/输入行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆