问题描述
我正在试图弄清楚当屏幕首次加载时如何一直滚动到UICollectionView的底部。我可以在触摸状态栏时滚动到底部,但我希望能够在视图加载时自动执行此操作。如果我想在触摸状态栏时滚动到底部,下面的工作正常。
I'm trying to figure out how to scroll all the way to the bottom of a UICollectionView when the screen first loads. I'm able to scroll to the bottom when the status bar is touched, but I'd like to be able to do that automatically when the view loads as well. The below works fine if I want to scroll to the bottom when the status bar is touched.
- (BOOL)scrollViewShouldScrollToTop:(UITableView *)tableView
{
NSLog(@"Detect status bar is touched.");
[self scrollToBottom];
return NO;
}
-(void)scrollToBottom
{//Scrolls to bottom of scroller
CGPoint bottomOffset = CGPointMake(0, collectionViewReload.contentSize.height - collectionViewReload.bounds.size.height);
[collectionViewReload setContentOffset:bottomOffset animated:NO];
}
我试过在viewDidLoad中调用[self scrollToBottom]。这不起作用。关于如何在视图加载时滚动到底部的任何想法?
I've tried calling [self scrollToBottom] in the viewDidLoad. This isn't working. Any ideas on how I can scroll to the bottom when the view loads?
推荐答案
只是详细说明我的评论。
Just to elaborate on my comment.
在元素可视化之前调用viewDidLoad,因此无法很好地操作某些UI元素。比如在工作中移动按钮但处理子视图通常不会(比如滚动CollectionView)。
viewDidLoad is called before elements are visual so certain UI elements cannot be manipulated very well. Things like moving buttons around work but dealing with subviews often does not (like scrolling a CollectionView).
在viewWillAppear或viewDidAppear中调用时,大多数这些操作都会起作用。以下是Apple文档中的一个除了指出重写这些方法之一时要做的重要事项:
Most of these actions will work best when called in viewWillAppear or viewDidAppear. Here is an except from the Apple docs that points out an important thing to do when overriding either of these methods:
超级调用通常在自定义实现之前调用。 (所以重写方法中的第一行代码)。
The super call is generally called before custom implementations. (so the first line of code inside of the overridden methods).
这篇关于屏幕加载时,UICollectionView会自动滚动到底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!