2015-08-25 85 views
1

我有一個顯示12個圖像的UICollectionView。圖像不適合細胞,它們正在裁剪,但我希望它們能夠適應細胞而不會被裁剪。縮放圖像以適合 - iOS

,應該使圖像比例,以適應該代碼是:

cell.imageView.contentMode = UIViewContentMode.ScaleAspectFit 
cell.imageView.image = UIImage(named: name) 

這是我整個的viewController類和截圖代碼:

import UIKit 

class DressingRoomViewController: 
     UIViewController, 
     UICollectionViewDelegateFlowLayout, 
     UICollectionViewDataSource { 

    @IBOutlet weak var collectionView: UICollectionView! 

    let identifier = "cellIdentifier" 
    let dataSource = DataSource() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     collectionView.dataSource = self 
    } 

    override func viewDidAppear(animated: Bool) { 
     // Notes on the equation to get the cell size: 
     // cells per row = 6 
     // cell spacing = 10 
     // collectionView.layout.inset = 20 (10 left, 10 right) 
     let cellSize = (collectionView.collectionViewLayout 
      .collectionViewContentSize().width - 20)/6 - 10 

     let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 

     layout.sectionInset = UIEdgeInsets(top: 20, 
              left: 10, 
              bottom: 10, 
              right: 10) 

     layout.itemSize = CGSize(width: cellSize, height: cellSize) 

     collectionView.collectionViewLayout = layout 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    override func prepareForSegue( segue: UIStoryboardSegue, 
            sender: AnyObject?) { 
     if (segue.identifier == "dressingRoom2MyOutfits") { 
      let myOutfitsViewController = segue.destinationViewController 
       as! MyOutfitsViewController 
     } 
    } 
} 



// MARK:- UICollectionViewDataSource Delegate 
extension DressingRoomViewController : UICollectionViewDataSource { 

    func numberOfSectionsInCollectionView(
     collectionView: UICollectionView) -> Int { 
      return 1 
    } 

    func collectionView(collectionView: UICollectionView, 
     numberOfItemsInSection section: Int) -> Int { 
      return 12 
    } 

    func collectionView(collectionView: UICollectionView, 
     cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(
      identifier,forIndexPath:indexPath) as! FruitCell 

     let fruits: [Fruit] = dataSource.fruits 
     let fruit = fruits[indexPath.row] 

     let name = fruit.name! 

     cell.imageView.contentMode = UIViewContentMode.ScaleAspectFit 
     cell.imageView.image = UIImage(named: name) 

     return cell 
    } 
} 

編輯:這裏是FruitCell類,以防萬一你想知道。

class FruitCell: UICollectionViewCell { 

    @IBOutlet weak var imageView: UIImageView! 

} 

enter image description here

+0

更改內容模式。這裏是鏈接:http://stackoverflow.com/questions/14134035/how-to-manage-uiimageview-content-mode –

+0

@MihirOza我已經改變了UIImageView的contentMode。所以我嘗試添加UIImageView的autoresizingMask,但沒有做任何事情。 – BeniaminoBaggins

+0

這不僅僅是您之前的重複http://stackoverflow.com/questions/32194205/uicollectionview-cells-do-not-take-up-width-of-uicollectionview? – matt

回答

1

已被放置在一個UICollectionViewCell從界面生成一個UIImageView,不會有已編程初始化一個UICollectionViewFlowLayout的任何知識。因此,我設置的layout.sectionInset使得UICollectionViewCells變小,即使在界面構建器中創建的UIImageView受限於UICollectionViewCell的邊緣,UIImageView也沒有調整爲變小。

解決方法是以編程方式初始化UIImageView,並將大小設置爲單元格大小,並考慮單元格間距。

下面是代碼:

import UIKit 

class DressingRoomViewController: 
     UIViewController, 
     UICollectionViewDelegateFlowLayout, 
     UICollectionViewDataSource { 

    @IBOutlet weak var collectionView: UICollectionView! 

    let identifier = "cellIdentifier" 
    let dataSource = DataSource() 
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
    let cellSpacing: CGFloat = 2 
    let cellsPerRow: CGFloat = 6 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     collectionView.dataSource = self 
    } 

    override func viewDidAppear(animated: Bool) { 
     let cellSize = (collectionView.collectionViewLayout 
      .collectionViewContentSize().width/cellsPerRow) - (cellSpacing) 

     layout.itemSize = CGSize(width: cellSize, height: cellSize) 
     layout.minimumInteritemSpacing = cellSpacing 
     layout.minimumLineSpacing = cellSpacing 
     collectionView.collectionViewLayout = layout 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    override func prepareForSegue( segue: UIStoryboardSegue, 
            sender: AnyObject?) { 
     if (segue.identifier == "dressingRoom2MyOutfits") { 
      let myOutfitsViewController = segue.destinationViewController 
       as! MyOutfitsViewController 
     } 
    } 
} 



// MARK:- UICollectionViewDataSource Delegate 
extension DressingRoomViewController : UICollectionViewDataSource { 

    func numberOfSectionsInCollectionView(
     collectionView: UICollectionView) -> Int { 
      return 1 
    } 

    func collectionView(collectionView: UICollectionView, 
     numberOfItemsInSection section: Int) -> Int { 
      return 12 
    } 

    func collectionView(collectionView: UICollectionView, 
     cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(
      identifier,forIndexPath:indexPath) as! FruitCell 

     let fruits: [Fruit] = dataSource.fruits 
     let fruit = fruits[indexPath.row] 

     let name = fruit.name! 

     var imageView :UIImageView 
     imageView = UIImageView(frame:CGRectMake( 0, 
                0, 
      (collectionView.collectionViewLayout 
       .collectionViewContentSize().width/cellsPerRow) 
       - (cellSpacing), 
      (collectionView.collectionViewLayout 
       .collectionViewContentSize().width/cellsPerRow) 
       - (cellSpacing))) 

     imageView.contentMode = UIViewContentMode.ScaleAspectFit 
     imageView.image = UIImage(named: name) 
     imageView.backgroundColor = UIColor.redColor() 
     cell.addSubview(imageView) 

     return cell 
    } 
} 

編輯:我能夠通過用於高度約束創建的出口(在接口控制+拖動高度約束至剪切掉在UICollectionView底部的空的空間建設者在迅速的viewController文件)調用它heightConstraint,然後在viewDidAppear的底部添加的這兩行代碼:

self.heightConstraint.constant = collectionView.contentSize.height 
self.view.layoutIfNeeded() 

所以向您展示結果用白色背景上的UICollectionView並在UIImageViews的紅色背景,這裏是結果(也適用於其他所有設備的大小):的UIImageView的

enter image description here