UICollectionViewFlowLayout doesn't use integral frames

独自空忆成欢 提交于 2020-01-22 10:07:07

问题


I have recently started using UICollectionView, and am a bit confused about the UICollectionViewFlowLayout. It would seem that the frames for each cell in the collection view are calculated with equal space between each item. This causes the frames of some of the cells to have fractional positions, which will cause blurry labels and misaligned image pixels and so on.

I am surprised to find that there are no questions about this on stack overflow though, which makes me think I am doing something wrong. I have created a test project that demonstrates the problem quite simply:

https://github.com/rmaz/BlurryCollectionView

Is this really the standard behaviour? It seems to me that this makes the flow layout basically unusable without subclassing. Or am I missing something?


回答1:


Workaround: subclass UICollectionViewFlowLayout, override UICollectionViewLayout's -layoutAttributesForElementsInRect: and for every layout attributes make the frame integral:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray *allLayoutAttributes = [super layoutAttributesForElementsInRect:rect];
    for (UICollectionViewLayoutAttributes *layoutAttributes in allLayoutAttributes) {
        layoutAttributes.frame = CGRectIntegral(layoutAttributes.frame);
    }
    return allLayoutAttributes;
}

Note: iOS 7 UICollectionViewFlowLayout has been fixed to always use integral frames for its cells' frames. I recommend keeping the fix for iOS 6.x but conditionally deprecate it for iOS 7 and newer.

Best, Raphael



来源:https://stackoverflow.com/questions/15098886/uicollectionviewflowlayout-doesnt-use-integral-frames

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!