设置帧后UITextView内容偏移量发生变化 [英] UITextView content offset changes after setting frame

查看:191
本文介绍了设置帧后UITextView内容偏移量发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个与消息应用程序非常相似的视图 - 我在页面底部有一个子视图,其中包含 UITextView 以及用户类型并且到达行的末尾,文本视图以及包含它的视图应该向上扩展。

I'm building a view that's very similar to the messages app - I have a subview at the bottom of the page with a UITextView in it and as the user types and reaches the end of the line the text view as well as the view containing it should expand upward.

我的工作方式是 textViewDidChange:方法我调用我的布局函数,那就是

The way I have it working is that in the textViewDidChange: method I call my layout function, and that does

CGFloat textViewWidth = 200;
CGFloat textViewHeight = [self.textView sizeThatFits:CGSizeMake(textViewWidth, 2000)].height;
[self resizeParentWithTextViewSize:CGSizeMake(textViewWidth, textViewHeight)];

// And then the resize parent method eventually calls
textView.frame = CGRectMake(10, 10, textViewWidth, textViewHeight);

问题是当在行尾输入并且视图扩展时,我最终会得到一个文本视图中的10.5之类的任意 contentOffset.y ,因此文本全部移动到视图的顶部。奇怪的是,它在每一条线上都是交替的,所以第一次扩展会使y内容偏移向上移动,然后在下一行接近零,然后在下一行返回到10.5等等(不确定这是否有用或者只是我的价值观的一个奇怪的神器)。之后我可以将它设置为零,但它看起来很糟糕,因为有一个短暂的闪光,其中文本具有偏移值,然后它被移回到中间。

The problem is that when typing at the end of line and the view expands, I end up with an arbitrary contentOffset.y of something like 10.5 on the text view so the text is all shifted up to the top of the view. Weirdly, it's alternating on every other line, so expanding the first time leaves the y content offset shifted up, then at the next line it's close to zero, then back to 10.5 on the next line, etc. (not sure if that's helpful or just a strange artifact of my values). I can set it back to zero afterwards but it looks terrible because there's a brief flash where the text has the offset value and then it gets shifted back to the middle.

I已经读过,通常最好使用内容插入来进行滚动视图而不是更改框架,但我不知道如何做到这一点,因为我确实需要更改框架大小。

I've read that it's usually better to use content insets for scroll views rather than changing the frame, but I don't get how to do that because I do need to change the frame size as well.

如何在不发生这种情况的情况下调整 UITextView 的大小?我认为我可以设置文本视图不可滚动并修复问题,但我想了解发生了什么。

How can I resize the UITextView without this happening? I think I can get by with setting the text view not to be scrollable and that fixes the issue, but I'd like to understand what's going on.

推荐答案

设置 textView.scrollable = NO 让我调整文本视图的大小,没有任何奇怪的偏移,这是我能够弄清楚的唯一方法。我想这对普通场景来说并不是太多限制,如果你想让文本视图可滚动,你可能不需要动态调整它,因为用户可以随着内容的变化滚动。

Setting textView.scrollable = NO lets me resize the text view without any strange offsets, that's the only way I've been able to figure out. And I guess it's not too much of a limitation for common scenarios, if you want the text view to be scrollable you probably don't need to resize it on the fly since the user can scroll around as the content changes.

这篇关于设置帧后UITextView内容偏移量发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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