在iOS 7中,给定一个UICollectionView,你如何在底部启动它?想想iOS消息应用程序,当视图变得可见时,它总是从底部开始(最近的消息)。
答案 0 :(得分:5)
@awolf 你的解决方案很好! 但是不能正常使用自动布局。
你应该先调用[self.view layoutIfNeeded]! 完整的解决方案是:
for (var i=0; i<n; i++) {
var request = new XMLHttpRequest();
var urlApi = 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=' + artistList[i] + '&api_key=somApiKeyThatWorks&format=json';
console.log(urlApi);
request.open('GET', urling, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var data = JSON.parse(request.responseText);
console.log(data);
答案 1 :(得分:4)
问题是,如果您尝试在viewWillAppear中设置集合视图的contentOffset,则集合视图尚未呈现其项目。因此,self.collectionView.contentSize仍为{0,0}。解决方案是询问集合视图的布局以获取内容大小。
此外,您还要确保在contentSize高于集合视图的边界时才设置contentOffset。
完整的解决方案如下:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CGSize contentSize = [self.collectionView.collectionViewLayout collectionViewContentSize];
if (contentSize.height > self.collectionView.bounds.size.height) {
CGPoint targetContentOffset = CGPointMake(0.0f, contentSize.height - self.collectionView.bounds.size.height);
[self.collectionView setContentOffset:targetContentOffset];
}
}
答案 2 :(得分:2)
这对我有用,我认为这是一种现代方式。
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.collectionView!.scrollToItemAtIndexPath(indexForTheLast, atScrollPosition: UICollectionViewScrollPosition.Bottom, animated: false)
}
答案 3 :(得分:1)
yourCollectionView.contentOffset = CGPointMake(0, yourCollectionView.contentSize.height - yourCollectionView.bounds.size.height);
但请记住,只有在contentSize.height
&gt; bounds.size.height
。
答案 4 :(得分:0)
答案 5 :(得分:0)
它对我来说很完美(自动布局)
答案 6 :(得分:0)
在scrollToItemAtIndexPath:atScrollPosition:animated:
的{{1}}中添加了,以便collectionView将立即在底部加载