思路
對于iPhone 6 Plus之前的手機(jī),pt和px的比例是1:2工腋,而iPhone 6 Plus出來之后,這一比例達(dá)到了1:3命满,
舉例:一個(gè)collectionView寬度是屏幕寬度 四等分
eg:6s 375 / 4 = 93.75
eg:6P 414 / 4 = 103.5
eg:6s 0.5個(gè)point 是1像素 多余025構(gòu)不成一個(gè)像素
eg:6P 1 / 3個(gè)point是1像素 多余0.166構(gòu)不成一個(gè)像素
只要是整數(shù)后面的小數(shù)是(1 / [UIScreen mainScreen].scale)就行
代碼
- (CGFloat)fixSlitWith:(CGRect)rect colCount:(CGFloat)colCount space:(CGFloat)space {
CGFloat totalSpace = (colCount - 1) * space;//總間隙
//每個(gè)item的寬度
CGFloat itemWidth = (rect.size.width - totalSpace) / colCount;
CGFloat fixValue = 1 / [UIScreen mainScreen].scale;
CGFloat realItemWidth = floor(itemWidth) + fixValue;//取整加fixValue floor:如果參數(shù)是小數(shù),則求最大的整數(shù)但不大于本身.
CGFloat realWidth = colCount * realItemWidth + totalSpace;
//算出屏幕等分后滿足1px=([UIScreen mainScreen].scale)pt實(shí)際的寬度,可能會(huì)超出屏幕,需要調(diào)整一下frame
CGFloat pointX = (realWidth - rect.size.width) / 2; //偏移距離
if (pointX > 0) {
rect.origin.x += pointX;//向左偏移
}else{
rect.origin.x -= pointX;//向左偏移
}
rect.size.width = realWidth;
self.collectionView.frame = rect;
[self.collectionView setNeedsLayout];
return realItemWidth; //每個(gè)cell的真實(shí)寬度
}