【发布时间】:2015-11-24 00:34:32
【问题描述】:
我有一个包含 UICollectionView 的应用。
当collectionView 首次出现时,可以看到每个单元格中包含的图像从看起来像CGRectZero 的大小调整为最终大小。
所有单元格 imageView 在情节提要中明确调整为恒定的高度和宽度,在 sizeForItemAtIndexPath 中我明确调整单元格的大小。
只是想知道是否有其他人遇到过这个问题并找到了解决方案。根据我的阅读,我需要添加 layoutIfNeeded。我已经尝试将它添加到 viewWillAppear 但它没有帮助。任何帮助将不胜感激。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Image Cell", forIndexPath: indexPath) as! ImageCollectionViewCell
cell.messageDateLabel.text = dateString
if let imageData = message.image {
let messageImage = UIImage(data: imageData)
cell.messageImage = messageImage
}
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let size = CGSizeMake(self.chatCollectionView.frame.size.width, Storyboard.ImageHeight)
return size
}
collectionView在containerView中呈现如下:
override func viewDidLoad() {
super.viewDidLoad()
imageCollectionViewController = self.storyboard?.instantiateViewControllerWithIdentifier("imageCollectionViewController") as? imageCollectionViewController
self.imageCollectionViewController!.view.translatesAutoresizingMaskIntoConstraints = false
self.addChildViewController(self.imageCollectionViewController!)
self.addSubview(self.imageCollectionViewController!.view, toView: self.containerView)
self.tabBarController?.tabBar.hidden = true
}
【问题讨论】:
-
谢谢。刚刚提供的代码。 collectionView 和 collectionView 单元格在故事板中设置。
-
它总是正确的,正如预期的那样,因为它本质上返回一个恒定的高度值和相同的 superView 的宽度。
-
我在返回size之前试过了
-
这是另一个问题:您说“所有单元格都在情节提要中明确调整大小”。你这是什么意思?
-
您没有正确地“添加子视图控制器”跳舞。你没有给
self.imageCollectionViewController!.view任何框架或约束,你也没有调用didMoveToParentViewController。
标签: ios swift uicollectionview uicollectionviewcell