最初,我有一個密集的控制器,其中包含一個delegate
和datasource
以及其他與呈現我的畫廊的CollectionView
相關的其他內容。爲了清理代碼,我將CollectionView
轉換爲基於.xib
的自定義創建視圖,這裏的關鍵點在於很多數字量保持不變。自定義基於.xib的CollectionView超出預期界限
以前,一切正常加載,每行3個縮略圖。但是,現在,每行仍有3個縮略圖,這些行溢出屏幕的右側,實際上是在屏幕之外。
我的看法是加載,這樣的:當我與它下面的註釋行替換gridPhotoGalleryView.frame = self.bounds
@IBOutlet var gridPhotoGalleryView: UICollectionView!
// MARK: Initialization
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
NSBundle.mainBundle().loadNibNamed("GridPhotoGalleryView", owner: self, options: nil)
gridPhotoGalleryView.frame = self.bounds
// gridPhotoGalleryView.frame = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, UIScreen.mainScreen().bounds.size.width + 5, self.bounds.height)
self.addSubview(self.gridPhotoGalleryView)
self.gridPhotoGalleryView.delegate = self
self.gridPhotoGalleryView.dataSource = self
self.gridPhotoGalleryView.registerNib(UINib(nibName: "ThumbnailCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: galleryPhotoCellIdentifier)
}
我的問題得到解決。
這裏發生了什麼?我的猜測是,這與加載該視圖的控制器的約束有關,但這在我的任何其他視圖中都不是問題。這個看似隨意的寬度是什麼,這個幻數讓我的3個縮略圖圖像在屏幕上完美匹配?
不確定是否有必要,但這裏有更多的代碼w.r.t.數據源。
let galleryThumbnailSize = UIScreen.mainScreen().bounds.size.width/3 - 3
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: ThumbnailCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(galleryPhotoCellIdentifier, forIndexPath: indexPath) as! ThumbnailCollectionViewCell
let asset: PHAsset = photosAsset!.objectAtIndex(indexPath.item) as! PHAsset
let retinaMultiplier: CGFloat = UIScreen.mainScreen().scale
let retinaSquare: CGSize = CGSizeMake(cell.bounds.size.width * retinaMultiplier, cell.bounds.size.height * retinaMultiplier)
PHImageManager.defaultManager().requestImageForAsset(
asset,
targetSize: retinaSquare,
contentMode: .AspectFill,
options: nil,
resultHandler: {(result, _) in cell.setThumbnailImage(result!, thumbnailSize: self.galleryThumbnailSize)})
return cell
}
非常徹底的迴應,這有助於理解iOS如何呈現內容 – mike