无论iPhone屏幕尺寸如何,我都希望连续显示两个单元格。喜欢,
我的情节提要中包含UICollectionView
,由约束连接。
现在,当我在6、5s或更短的时间内运行此程序时,一行中只会出现一个单元格。我使用的代码是,
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [categoryImages count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
homePageCells *cell = (homePageCells *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cells" forIndexPath:indexPath];
cell.categoryName.text = [categoryNames objectAtIndex:indexPath.row];
return cell;
}
我尝试使用代码检测屏幕尺寸并以编程方式分配适当的像元尺寸,
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize sizes;
CGSize result = [[UIScreen mainScreen] bounds].size;
NSLog(@"%f",result.height);
if(result.height == 480)
{
//Load 3.5 inch xib
sizes =CGSizeMake(170.f, 170.f);
NSLog(@"3gs");
}
else if(result.height == 568)
{
//Load 4 inch xib
sizes =CGSizeMake(130.f, 130.f);
NSLog(@"5s");
}
else if(result.height == 667.000000)
{
//Load 4.7 inch xib
sizes =CGSizeMake(160.f, 160.f);
NSLog(@"6");
}
else
{
NSLog(@"else");
sizes =CGSizeMake(170.f, 165.f);
}
return sizes;
}
但是我也知道这不是正确的方法,所以请给我正确的方法来解决这个问题。
您无需检查设备尺寸,因为我们可以使用collectionView
宽度来计算单元格的宽度。使用像元宽度,您可以根据需要计算高度。
另外一件事:您需要使用UICollectionViewDelegateFlowLayout
并确认下面的委托和实现方法
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let padding: CGFloat = 50
let collectionViewSize = collectionView.frame.size.width - padding
return CGSizeMake(collectionViewSize/2, collectionViewSize/2)
}
更新:Swift 4.0
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let padding: CGFloat = 50
let collectionViewSize = collectionView.frame.size.width - padding
return CGSize(width: collectionViewSize/2, height: collectionViewSize/2)
}
[Note:考虑到单元格是方形的,我已经回答了这个问题
Xcode 8:Swift 3
我知道这是一个比较老的问题,但是我发现了一些可以接受的答案。
我必须使用UICollectionViewDelegateFlowLayout,而不是CollectionViewFlowLayout
然后
在其中,'Padding'是Int类型的,当您尝试从变量collectionViewSize中减去它时,会抛出错误,因为它们是不同的类型。但是很容易修复。
我刚刚在行中添加了CGFloat
最后,虽然可能有更好的方法,但我不得不通过调整collectionView的前导和尾随约束来匹配填充。
因此,这就是我的代码最终的样子
extension LandingPageViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let padding: CGFloat = 25
let collectionCellSize = collectionView.frame.size.width - padding
return CGSize(width: collectionCellSize/2, height: collectionCellSize/2)
}
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat padding = 50;
CGFloat cellSize = collectionView.frame.size.width - padding;
return CGSizeMake(cellSize / 2, cellSize / 2);
}
Xcode 11-Swift 4更新
基于:
将以下扩展添加到您的UICollectionViewController:
class MyCollectionController: UICollectionViewController {
// your code...
}
extension MyCollectionController : UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let padding: CGFloat = 8
let collectionViewSize = collectionView.frame.size.width - padding
return CGSize(width: collectionViewSize/2, height: collectionViewSize/2)
}
}
我当时传递给sizeForItemAtIndexPath
的collectionView的边界不正确,因此出现问题。
对此的一种解决方法是重新加载viewWillAppear
中的单元格,以确保collectionView的边界正确。
另一个是使用UIScreen
类。
-(instancetype)init: {
self = [super init];
if (self) {
width = [[UIScreen mainScreen] bounds].size.width;
}
return self;
}
-(CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
// Two columns with spacing in between
CGFloat size = ((width / 2) - (kSpacing * 2));
return CGSizeMake(size, size);
}
在这种情况下,我将委托作为一个单独的类实例化(因此初始化),但是它可以像扩展一样容易地完成。