原因
當
UICollectionViewCell
的size不為整數(shù)時,UICollectionViewFlowLayout
在布局計算時,可能會調整Cell的frame.origin,使Cell按照最小物理像素(渲染像素)對齊,導致出現(xiàn)縫隙。假設當前Cell的frame.origin.y=100.8(邏輯像素),轉化成渲染像素(參考備注)是201.6(iPhone 8)或302.4(iPhone 8 Plus)名秀。為了按渲染像素對齊,
UICollectionViewFlowLayout
應該會四舍五入取整藕溅,取整后為202(iPhone 8)或302(iPhone 8 Plus)匕得,轉成邏輯像素101(iPhone 8)或100.667(iPhone 8 Plus),導致在iphone8上就會出現(xiàn)0.2像素的縫隙巾表。分辨率相關的汁掠,可以百度下。
簡單解決辦法:
- 主動把Cell的size取整集币,不丟給
UICollectionViewFlowLayout
處理考阱。
- (CGSize)fixedCollectionCellSize:(CGSize)size {
CGFloat scale = [UIScreen mainScreen].scale;
return CGSizeMake(round(scale * size.width) / scale, round(scale * size.height) / scale);
}
Demo實驗
- UITableView,Cell高度設置成100.12鞠苟,沒有強制被按渲染像素對齊乞榨,如
99.62
1802.16
1902.28
(lldb) po [0x7fb85b83b800 recursiveDescription]
<UITableView: 0x7fb85b83b800; frame = (0 0; 375 667); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x6000004451f0>; layer = <CALayer: 0x60000022dc40>; contentOffset: {0, 1272.5}; contentSize: {375, 2002.4000549316406}; adjustedContentInset: {20, 0, 0, 0}>
| <UITableViewCell: 0x7fb85c862a00; frame = (0 1902.28; 375 100.12); autoresize = W; layer = <CALayer: 0x6040004265a0>>
| | <UITableViewCellContentView: 0x7fb85b70fea0; frame = (0 0; 375 99.62); gestureRecognizers = <NSArray: 0x6040002426a0>; layer = <CALayer: 0x604000425e80>>
| | | <UIImageView: 0x7fb85b41a330; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <CALayer: 0x6000008215c0>>
| | <_UITableViewCellSeparatorView: 0x7fb85b710820; frame = (15 99.62; 360 0.5); layer = <CALayer: 0x604000427640>>
| <UITableViewCell: 0x7fb85c862400; frame = (0 1802.16; 375 100.12); autoresize = W; layer = <CALayer: 0x6040002212e0>>
- iPhone Plus 8,UICollectionView当娱,Cell高度設置成100.12吃既,強制被按渲染像素對齊了,frame.origin.y被調整后的值趾访,如
200.333
500.667
600.667
| <UICollectionViewCell: 0x7fdd9f71f010; frame = (0 200.333; 414 100.12); hidden = YES; layer = <CALayer: 0x60000023d800>>
| <UICollectionViewCell: 0x7fdd9f71f470; frame = (0 300.333; 414 100.12); layer = <CALayer: 0x60000023e380>>
| <UICollectionViewCell: 0x7fdd9f72aa30; frame = (0 400.333; 414 100.12); layer = <CALayer: 0x60000023f4e0>>
| <UICollectionViewCell: 0x7fdd9f72ae90; frame = (0 500.667; 414 100.12); layer = <CALayer: 0x60000023f9a0>>
| <UICollectionViewCell: 0x7fdd9f72b510; frame = (0 600.667; 414 100.12); layer = <CALayer: 0x60000023fbc0>>
| <UICollectionViewCell: 0x7fdd9f72d1a0; frame = (0 701; 414 100.12); layer = <CALayer: 0x60000023fdc0>>
- iPhone Plus态秧,UICollectionView董虱,Cell高度設置成100.12扼鞋,強制被按渲染像素對齊了,frame.origin.y被調整后的值愤诱,如
300.5
400.5
500.5
| <UICollectionViewCell: 0x7f85b441b6c0; frame = (0 0; 375 100.12); hidden = YES; layer = <CALayer: 0x600000035ec0>>
| <UICollectionViewCell: 0x7f85b4502e10; frame = (0 100; 375 100.12); hidden = YES; layer = <CALayer: 0x600000035c20>>
| <UICollectionViewCell: 0x7f85b4505c10; frame = (0 200; 375 100.12); layer = <CALayer: 0x600000036300>>
| <UICollectionViewCell: 0x7f85b4506400; frame = (0 300.5; 375 100.12); layer = <CALayer: 0x600000036be0>>
| <UICollectionViewCell: 0x7f85b4506d70; frame = (0 400.5; 375 100.12); layer = <CALayer: 0x600000227ac0>>
| <UICollectionViewCell: 0x7f85b4507560; frame = (0 500.5; 375 100.12); layer = <CALayer: 0x6000002277c0>>
備注
Points(邏輯像素)<--->Rendered Pixels(渲染像素)<--->Physical Pixels(物理像素)
iOS不同機型尺寸.png