当我的文本没有填充 UITextView 时,它会按预期滚动到顶部.当文本超出屏幕大小时,UITextView 会滚动到文本的中间,而不是顶部.
When I have text that does not fill the UITextView, it is scrolled to the top working as intended. When there is more text than will fit on screen, the UITextView is scrolled to the middle of the text, rather than the top.
以下是一些可能相关的详细信息:
Here are some potentially relevant details:
在 viewDidLoad 中为 UITextView 的顶部和底部提供一些填充:
In viewDidLoad to give some padding on top and bottom of UITextView:
self.mainTextView.textContainerInset = UIEdgeInsetsMake(90, 0, 70, 0);
UITextView 使用自动布局将其从屏幕的顶部、底部和每一侧锚定 20 像素(在 IB 中完成),以允许不同的屏幕尺寸和方向.
The UITextView uses auto layout to anchor it 20px from top, bottom and each side of the screen (done in IB) to allow for different screen sizes and orientations.
加载后我仍然可以用手指滚动它.
I can still scroll it with my finger once its loaded.
编辑我发现删除自动布局约束然后修复宽度似乎只能解决问题,但仅限于该屏幕宽度.
EDIT I found that removing the auto layout constraints and then fixing the width only seems to fix the issue, but only for that screen width.
UITextView 是 UIScrollView 的子类,所以你可以使用它的方法.如果您只想确保将其滚动到顶部,那么无论添加文本的位置如何:
UITextView is a subclass of UIScrollView, so you can use its methods. If all you want to do is ensure that it's scrolled to the top, then wherever the text is added try:
[self.mainTextView setContentOffset:CGPointZero animated:NO];
任何类型的滚动视图的自动布局都会很快变得不稳定.设置固定宽度解决了它并不奇怪.如果它在 -viewDidLayoutSubviews
中不起作用,那很奇怪.手动设置布局约束可能有效.首先在 IB 中创建约束:
AutoLayout with any kind of scrollview gets wonky fast. That setting a fixed width solves it isn't surprising. If it doesn't work in -viewDidLayoutSubviews
then that is odd. Setting a layout constraint manually may work. First create the constraints in IB:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewWidthConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewHeightConstraint;
然后在 ViewController 中
then in the ViewController
-(void)updateViewConstraints {
self.textViewWidthConstraint.constant = self.view.frame.size.width - 40.0f;
self.textViewHeightConstraint.constant = self.view.frame.size.height - 40.0f;
[super updateViewConstraints];
}
可能仍然需要在 -viewDidLayoutSubviews
中设置ContentOffset.
May still be necessary to setContentOffset in -viewDidLayoutSubviews
.
(另一种方法是在 textView 和它的 superView 之间为'equal' widths"和'equal'heights"创建一个布局约束,常量为-40".如果常量为零,否则按常量调整.但是因为只能将此约束添加到同时约束两个视图的视图中,所以不能在 IB 中执行此操作.)
(Another method would be to create a layout constraint for "'equal' widths" and "'equal' heights" between the textView and its superView, with a constant of "-40". It's only 'equal' if the constant is zero, otherwise it adjusts by the constant. But because you can only add this constraint to a view that constraints both views, you can't do this in IB.)
您可能会问自己,如果我必须这样做,那么 AutoLayout 的意义何在?我已经深入研究了 AutoLayout,这是一个很好的问题.
You may ask yourself, if I have to do this, what's the point of AutoLayout? I've studied AutoLayout in depth, and that is an excellent question.
这篇关于UITextView 加载时不滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!