本文介绍了UITextView的不滚动到顶部加载时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的文字,不填的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的使用自动布局从顶部,底部和屏幕(IB中完成)的每一侧20像素固定它允许不同的屏幕尺寸和方向。

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.

编辑
我发现,除去汽车布局限制,然后固定宽度似乎只解决这个问题,但仅适用于屏幕宽度。

EDITI 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];
        }

仍可能需要在setContentOffset -viewDidLayoutSubviews

(另一种方法是创建平等宽度和TextView中和它的父之间的'平等'的高度一个约束布局,以-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.)

您可能会问自己,如果我要做到这一点,什么是自动版式的地步?我研究的自动版式的深度,这是一个非常好的问题。

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的不滚动到顶部加载时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 00:37