UICollectionView scrollToItemAtIndexPath [英] UICollectionView scrollToItemAtIndexPath

查看:1301
本文介绍了UICollectionView scrollToItemAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试滚动到集合视图中的特定项目,它似乎在90%的时间内正常发生。我基本上有一个集合视图,其框架我通过自动布局更改,然后我希望我的单元格是所有新框架的大小,所以我设置了一个新的大小。当我在scrollToItemAtIndexPath上放置一个断点时,看起来当它工作时,项目大小已经生效,但是它不起作用的时间,单元格仍然具有旧的大小。如何在滚动之前确保itemSize已更改?

I'm trying to scroll to a specific item in a collection view and it seems to happening properly about 90 % of the time. I basically have a collection view whose frame I change via auto layout and then I want my cell be the size of the all of the new frame, so I set a new size. When I put a breakpoint on the scrollToItemAtIndexPath, it seems when it works works the item size have taken effect, but the times it doesn't work, the cell still have the old size. How can I make sure the itemSize has changed before scrolling?

[weakSelf.view removeConstraints:@[weakSelf.verticalSpace, weakSelf.collectionViewBottomSpace, weakSelf.bottomLayoutTopCollectionView]];
[weakSelf.view addConstraints:@[weakSelf.collectionViewToTop, weakSelf.imageHeight, weakSelf.youLabelToTop]];
[UIView animateWithDuration:animated ? .5 : 0.0
                                      animations:^{
                                          [weakSelf.view layoutIfNeeded];
                                      }
                                      completion:^(BOOL finished) {
  UICollectionViewFlowLayout * layout = (UICollectionViewFlowLayout *)self.currentUserCollectionView.collectionViewLayout;

  layout.itemSize = weakSelf.currentUserCollectionView.frame.size;

  [weakSelf.currentUserCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:[self getSelectedIndex:selectItem] inSection:0]
                                         atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
                                                 animated:NO];
}];


推荐答案

确保集合视图首先列出其子视图在滚动之前调整单元格的大小。我建议将你的滚动移动到你确定所有布局都已完成的地方,例如:

Make sure the collection view has laid out its subviews first along with the resizing of the cell before scrolling. I would suggest moving your scrolling to a place where you're sure all layouts have been completed such as:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    NSIndexPath *indexPath = // compute some index path

    [self.collectionView scrollToItemAtIndexPath:indexPath
                                atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
                                        animated:YES];
}

这篇关于UICollectionView scrollToItemAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆