I'm trying to create a grid style view that displays photo album covers. I would like 3 album covers per row. The tricky part is getting the cells to increase size appropriately depending on what device is being used e.g. iPhone 4s/5/5s/5c/6/6 plus, so that 3 items/cells are always shown per row no matter what device is being used.
我正在尝试创建一个显示相册封面的网格样式视图。我想每排3张专辑封面。棘手的部分是让细胞适当地增加尺寸,这取决于所使用的装置,例如iPhone 4s / 5 / 5s / 5c / 6/6 plus,无论使用何种设备,每行始终显示3个/单元格。
I've never been able to do this. I was thinking about setting the sizes manually like so:
我从来没能做到这一点。我正在考虑手动设置尺寸:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if DeviceHelper.DeviceType.IS_IPHONE_4_OR_LESS {
return CGSizeMake(115, 140)
} else if DeviceHelper.DeviceType.IS_IPHONE_5 {
return //size
} else if DeviceHelper.DeviceType.IS_IPHONE_6 {
return //size
} else {
return //size
}
}
I have a helper that determines what device is being used and I can call it from anywhere within my app. But isn't there a way to make sure 3 cells are always displayed in each row no matter what device is being used and also have equal padding shown to something like this:
我有一个帮助器,确定正在使用的设备,我可以在我的应用程序中的任何地方调用它。但是有没有办法确保无论使用什么设备,每行中都会显示3个单元格,并且还有相同的填充显示如下:
The aspect ratio constraints have been helpful but this time round I can't apply them to the cell because interface builder won't allow it. Is there a straight forward to do this?
宽高比约束很有帮助,但这次我不能将它们应用于单元格,因为界面构建器不允许它。有没有直接做到这一点?
Thanks in advance for your time.
在此先感谢您的时间。
1 个解决方案
#1
Remove default cell spacing :
删除默认单元格间距:
Try this code :
试试这段代码:
Just set value for numberOfCellInRow
for number of cells in a row of collection view
只需为numberOfCellInRow设置一行集合视图中的单元格数
Obj-C
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
int numberOfCellInRow = 3;
CGFloat padding = 5.0;
CGFloat collectionCellWidth = [[UIScreen mainScreen] bounds].size.width/numberOfCellInRow;
CGFloat finalWidthWithPadding = collectionCellWidth - padding;
return CGSizeMake(finalWidthWithPadding , finalWidthWithPadding);
}
Swift
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var numberOfCellInRow : Int = 3
var padding : Int = 5
var collectionCellWidth : CGFloat = (self.view.frame.size.width/CGFloat(numberOfCellInRow)) - CGFloat(padding)
return CGSize(width: collectionCellWidth , height: collectionCellWidth)
}
I think it will work for all the devices.
我认为它适用于所有设备。
#1
Remove default cell spacing :
删除默认单元格间距:
Try this code :
试试这段代码:
Just set value for numberOfCellInRow
for number of cells in a row of collection view
只需为numberOfCellInRow设置一行集合视图中的单元格数
Obj-C
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
int numberOfCellInRow = 3;
CGFloat padding = 5.0;
CGFloat collectionCellWidth = [[UIScreen mainScreen] bounds].size.width/numberOfCellInRow;
CGFloat finalWidthWithPadding = collectionCellWidth - padding;
return CGSizeMake(finalWidthWithPadding , finalWidthWithPadding);
}
Swift
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var numberOfCellInRow : Int = 3
var padding : Int = 5
var collectionCellWidth : CGFloat = (self.view.frame.size.width/CGFloat(numberOfCellInRow)) - CGFloat(padding)
return CGSize(width: collectionCellWidth , height: collectionCellWidth)
}
I think it will work for all the devices.
我认为它适用于所有设备。