简体   繁体   English

在scrollview中使文本从顶部开始,而不是从底部开始(Objective-C)

[英]Getting text in scrollview to start at the top, not the bottom (Objective-C)

In my view, I load some text content in from a database within the viewDidLoad section of code. 在我看来,我从代码的viewDidLoad部分中的数据库中加载了一些文本内容。

This works fine and looks fine, but the scrollview sits at the very bottom of the text when the app is run. 这可以正常工作,看起来不错,但是在运行应用程序时,滚动视图位于文本的最底部。

I added this code below: 我在下面添加了以下代码:

-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear: animated];
[self.descriptionTextV setContentOffset:CGPointZero animated:YES];
}

And this now moves the scroll view to the top of the text, which is what I want, but it takes a couple of seconds to do this, and it is distracting. 现在,这会将滚动视图移动到文本的顶部,这正是我想要的,但是这需要花费几秒钟的时间,而且分散了注意力。

I have tried 我努力了

[self.descriptionTextV setContentOffset:CGPointZero animated:YES];

Which moves the text with an animated scroll over a few seconds. 通过动画滚动文本将文本移动几秒钟。

[self.descriptionTextV setContentOffset:CGPointZero animated:NO];

Which 'jumps' the text from the bottom to the top. 从底部到顶部“跳转”文本。

And I have tried moving the setContentOffset command to the viewDidLoad and to a viewWillAppear section, but then it doesn't work at all. 而且我尝试将setContentOffset命令移到viewDidLoadviewWillAppear部分,但是然后根本不起作用。

Is there any way to get my text to start at the TOP of the text, not the bottom, without it animating, scrolling or jumping? 有没有办法让我的文本在没有动画,滚动或跳跃的情况下从文本的顶部(而不是底部)开始?

I just want the text to start at the top, and for there to be no visual jump that the user can see in the final app. 我只希望文本从顶部开始,并且没有在最终应用程序中用户可以看到的视觉跳跃。

If I had the correct understanding out of your problem, you're using a UITextView to load your text into, what makes this problem really odd, since the default behavior of UITextView is to start from top. 如果我对您的问题有正确的了解,那么您正在使用UITextView将文本加载到其中,这真使这个问题很奇怪,因为UITextView的默认行为是从顶部开始。 If this is the case, make sure that no other actions is being performed on the text view. 在这种情况下,请确保没有在文本视图上执行其他任何操作。 Also, you might want take a look on how long you're taking to load your data. 另外,您可能需要查看加载数据需要花费多长时间。

I have now managed to get this working by moving my code to the following method: 现在,我通过将代码移至以下方法来设法解决此问题:

-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [self.descriptionTextV setContentOffset:CGPointZero animated:NO];
}

And this works great! 这很棒!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM