ios - UICollectionViewCell 位置翻倍
<p><p>我刚刚注意到 uicollectionviewcells 的中心是真实中心的一半。</p>
<p>示例:</p>
<p>将单元格的框架设置为:</p>
<pre><code> {{0, 0}, {100, 100}}
{{100, 100}, {100, 100}}
{{200, 200}, {100, 100}}
{{300, 300}, {100, 100}}
{{400, 400}, {100, 100}}
{{500, 500}, {100, 100}}
</code></pre>
<p>注意:这是我从 setFrame 得到的值,<a href="http://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionReusableView_class/Reference/Reference.html#//apple_ref/occ/instm/UICollectionReusableView/applyLayoutAttributes:" rel="noreferrer noopener nofollow">applyLayoutAttributes:</a> </p>
<p>看起来像这样:
<img src="/image/9y6pS.png" alt="actual look"/> </p>
<p>但应该是这样的:
<img src="/image/Q2H78.png" alt="how it should look"/> </p>
<h2>UICollectionViewLayout子类的实现</h2>
<pre><code>@implementation CVTLayout
- (void)prepareLayout {
;
CollectionViewTable *cvt = (CollectionViewTable*)self.collectionView;
CGSize size = CGSizeMake(100, 100);
CGPoint center = CGPointMake(50, 50);
NSMutableArray *cellAttributes = [ init];
for (int item = 0; item < ; item++) {
NSIndexPath *indexPath = ;
UICollectionViewLayoutAttributes *attributes = ;
attributes.size = size;
attributes.center = center;
;
center.x += size.width;
center.y += size.height;
}
cvt.cellAttributes = cellAttributes;
}
- (CGSize)collectionViewContentSize {
return self.collectionView.bounds.size;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewTable *cvt = (CollectionViewTable*)self.collectionView;
return cvt.cellAttributes;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray *layoutAttributes = [ init];
CollectionViewTable *cvt = (CollectionViewTable*)self.collectionView;
for (UICollectionViewLayoutAttributes *cellAttributes in cvt.cellAttributes) {
if (CGRectIntersectsRect(rect, cellAttributes.frame)) {
;
}
}
return layoutAttributes;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return false;
}
@end
</code></pre>
<hr/></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>问题是将 cell-label-frame 设置为 cell-frame 而不是 cell-bounds...</p></p>
<p style="font-size: 20px;">关于ios - UICollectionViewCell 位置翻倍,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/16961009/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/16961009/
</a>
</p>
页:
[1]