2016-06-09 70 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中設置約束,它會自動改變圖像的大小,然後使用我的代碼,它將起作用。 –

相關問題