2016-06-09 114 views
0

enter image description hereUICollectionView单元格大小调整

右列图片被截断。例如,右边的粉色狮子应该基本对称,但显然不是。这是我配置imageCell的方式。

func configureCellWithImage(image: Image){ 
    let iV = self.imageView 
    iV.setImageWithURL(image.thumbURL) 
    if(image.aspectRatio < 1.62){ 
     iV.contentMode = UIViewContentMode.ScaleAspectFit 
    }else{ 
     iV.contentMode = UIViewContentMode.ScaleAspectFill 
    } 

} 

这就是我如何返回单元格的大小。

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    let size = CGSize.init(width: screenSize.width/2 - 1, height: screenSize.width/2 * 1.62 - 1) 
    return size 
} 

帮助感谢。谢谢。

编辑:

我不知道为什么UIView的是那里的201宽度应该是159.我将如何访问这个观点?我应该只有CollectionView,imageCell和UIImageView。

enter image description here

+0

你确定,你的收藏视图布局'minimumInteritemSpacing'是1? – Johnykutty

回答

0

你已经在sizeForItemAtIndexPath方法使用屏幕大小,这是错误的,你会使用collectionView的宽度尺寸,使用我的下面的代码,

func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize 
     { 
      return CGSize(width: (self.collectionView!.frame.width/2)-5, height: ((self.collectionView!.frame.width/2) * 1.62) - 1) 
     } 

    func collectionView(collectionView: UICollectionView, 
     layout collectionViewLayout: UICollectionViewLayout, 
     insetForSectionAtIndex section: Int) -> UIEdgeInsets { 
      return UIEdgeInsetsMake(0, 0, 0, 0) 
    } 

    func collectionView(collectionView: UICollectionView, 
     layout collectionViewLayout: UICollectionViewLayout, 
     minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat { 
      return 0.0 
    } 

    func collectionView(collectionView: UICollectionView, 
     layout collectionViewLayout: UICollectionViewLayout, 
     minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat { 
      return 0.0 
    } 

希望它有帮助。

+0

不幸的是,这并没有奏效。它看起来像编译器正在创建另一个UIView。请参阅我的OP上的编辑。 – Latcie

+0

你将在'UICollectionViewCell'中设置imageView,并在ImageView中设置约束,它会自动改变图像的大小,然后使用我的代码,它将起作用。 –

相关问题