最初に私はdelegate
とdatasource
と多くの他の多くのものが私のギャラリーを描画するCollectionView
に関連する高密度コントローラを持っていました。コードを少しきれいにするために、私は.xib
に基づいてカスタム作成ビューにCollectionView
を移動しました。ここでの重要な点は、同じ数量が残っています。カスタム.xibベースのCollectionViewが予想した範囲を超えています
以前はすべてが細かく読み込まれ、1行に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)
}
私の問題が修正されます。
ここで何が起こっているか私の推測では、これは何とかこのビューをロードするコントローラのAutoLayout
の制約に関連していますが、これは他のどのビューでも問題にはなりません。 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