简体   繁体   English

CollectionView的约束问题

[英]Constraints issues with CollectionView

All, I have a View Controller with a UICollectionView , and the UICollection has a collection of small images (35x35) to make a grid. 所有,我都有一个带有UICollectionView的View Controller,并且UICollection具有一个小图像(35x35)的集合以构成一个网格。 I am using an inferred size of VC. 我使用VC的推断大小。 I have set the top, bottom, left and right constraints.. 我设置了顶部,底部,左侧和右侧约束..

The problem is that on the iPhone 6 the amount of cells are correct, but obviously on a iPhone 5 and iPhone 4 there is a smaller amount vertically. 问题是在iPhone 6上的单元数量是正确的,但显然在iPhone 5和iPhone 4上垂直的数量较少。

How did I get it so that there is an exact amount of cells vertically on all devices ? 我是如何得到它以便在所有设备上垂直存在确切数量的单元格?

I am not familiar with swift, but i hope that this objective-c explanation helps: 我不熟悉swift,但我希望这个客观的解释有助于:

In your -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath method you can specify the width and height of your cell. 在您的-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath方法您可以指定单元格的宽度和高度。

for example if you only want 2 cells per row and 3 per column user this: 例如,如果您只希望每行2个单元格,每列用户3个单元格,则可以:

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
     CGRect screen = [[UIScreen mainScreen]bounds];
     return CGSizeMake(screen.size.width/2 - YourHorizontalPadding, screen.size.height/3- YourVerticalPadding);
}

The vertical and horizontal padding is the space you have between your cells, you will have to tweak them to get the desired result. 垂直和水平填充是单元格之间的空间,您必须对其进行调整才能获得所需的结果。

Hope that helps, let me know if something is not clear. 希望有所帮助,如果有什么不清楚,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM