【问题标题】:Show three components in one row of UICollectionView在 UICollectionView 的一行中显示三个组件
【发布时间】:2017-08-13 05:20:50
【问题描述】:

我必须在UICollectionView 的一行中显示三个组件我已经编写了以下代码来操作flowlayout

- (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)collectionView.collectionViewLayout;

    CGSize size = flowLayout.itemSize;
    size.width = (Get_Bounds.width-40)/3;
    size.height = 135;   
    return size;

}

这在所有iOS 设备上都可以正常工作,但在iPhone 6 Plus 上渲染效果不佳,它仅在此设备上渲染两个组件。

【问题讨论】:

标签: ios objective-c iphone uicollectionview uicollectionviewlayout


【解决方案1】:

sizeForItemAtIndexPath 方法中,您需要提供 collectionViewCell Item 的大小。

因此,使用此算法,我们将集合视图单元格尺寸除以 3,然后减去 15(每个元素为 5),与高度类似,反之亦然。

- (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

        return CGSize(width: (self.view.frame.size.width/3)-15, height: (self.view.frame.size.width/3)+15)

  }

【讨论】:

  • 上述实现如何适用于除 iPhone 6 plus 以外的所有其他设备?你能指出来吗?
【解决方案2】:

你试试那个代码

    let height = img.size.height
    let width = img.size.width
    let cellHeight = collectionView.frame.size.width/3 * height / width
    return CGSize(width: collectionView.frame.size.width/3, height: cellHeight)

获取设备大小:

func iPhoneScreenSizes(){
let bounds = UIScreen.mainScreen().bounds
let height = bounds.size.height

switch height {
case 480.0:
    print("iPhone 3,4")
case 568.0:
    print("iPhone 5")
case 667.0:
    print("iPhone 6")
case 736.0:
    print("iPhone 6+")

default:
    print("not an iPhone")

}

}

您需要创建约束出口并将其管理到设备相关

【讨论】:

  • 我想要一行中的三个组件!!
  • 不,我想依赖于屏幕的大小而不是其他任何东西!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-26
  • 1970-01-01
  • 2013-07-16
相关资源
最近更新 更多