如何根据设备屏幕大小调整collections视图单元格?

我注意到单元格会一直遵循大小检查器的定义。 即使我已经应用了UICollectionViewDelegateFlowLayout。

在这里输入图像说明

在这里输入图像说明

所以这就是它的样子。 但我希望细胞看起来像是如何在一个较小的iphone屏幕上。

在这里输入图像说明

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGFloat height = self.view.frame.size.height; CGFloat width = self.view.frame.size.width; // in case you you want the cell to be 40% of your controllers view return CGSizeMake(width*0.4,height*0.4) } 

一种方法是保持单元格的长宽比,并给出这样的大小:

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { if (screenheight < 667) { return CGSize(width: 155, height: 200) } else if (screenheight < 736) { return CGSize(width: 182, height: 220) } else { return CGSize(width: 200, height: 230) } } 

你应该尝试使用一个简单的公式,而不是像这样设置你的单元格大小:

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: view.frame.width / 2, height: 350) } 

将它除以2将意味着单元总是屏幕大小的一半,带走最小行间距函数和/或您调用的UIEdgeInset方法。 希望这可以帮助。

根据我的经验,您必须使用UICollectionView方法获取设备特定的集合视图单元大小,按照以下方法您可以获得dynamic宽度和高度集合视图单元格。

 // Dynamic width & height - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { float cellSize = collectionView.frame.size.width/values.count; return CGSizeMake(cellSize - 10, collectionView.frame.size.height); } //For top/bottom/left/right padding method - (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 0, 0, 1); } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { float width = collectionView.frame.size.width; float spacing = [self collectionView:collectionView layout:collectionViewLayout minimumInteritemSpacingForSectionAtIndex:section]; int numberOfCells = (width + spacing) / (cellSize + spacing); int inset = (width + spacing - numberOfCells * (cellSize + spacing) ) / 2; return UIEdgeInsetsMake(0, inset, 0, inset); } - (CGFloat)collectionView:(UICollectionView *) collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger) section { return 1.0; } 

希望这会帮助你根据设备的大小devise你的手机。

在swift 3中,您可以使用collectionViewLayout属性根据屏幕调整collection view view单元的大小。

  let numberOfCell = 3 let cellSpecing = 10 if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout { layout.itemSize = CGSize(width: (screenSize.width - (cellSpecing * (numberOfCell + 1))) / numberOfCell, height: cellHeight) layout.invalidateLayout() } 
  func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { let theWidth = (_screenSize.width - (15*3))/2 // it will generate 2 column let theHeight = (_screenSize.height - (10*2))/3 // it will generate 3 Row return CGSize(width: theWidth ,height: theHeight) }