【发布时间】:2013-11-06 15:34:29
【问题描述】:
我正在尝试滚动到集合视图中的特定项目,并且它似乎在大约 90 % 的时间里正常发生。我基本上有一个集合视图,我通过自动布局更改其框架,然后我希望我的单元格是所有新框架的大小,所以我设置了一个新大小。当我在 scrollToItemAtIndexPath 上放置断点时,似乎在它工作时项目大小已经生效,但它不起作用的时候,单元格仍然具有旧大小。如何在滚动之前确保 itemSize 已更改?
[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];
}];
【问题讨论】:
-
问题是什么?您刚刚在此处粘贴了代码。
-
抱歉...按得太快了
-
别担心,顺便说一句,不是我的反对票 :)
-
当你改变collection view的布局约束时,你是在调用
invalidateLayout吗? -
我调用 layoutIfNeeded。我更新了代码。
标签: ios