最初,我有一个密集的控制器,其中包含一个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