【问题标题】:cells content grows out from CGRectZero as they appear in collectionViewCell (iOS)当它们出现在collectionViewCell(iOS)中时,单元格内容从CGRectZero中长出来
【发布时间】: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


【解决方案1】:

你可以试试这个吗?我只是提出一个想法。

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
}
    cell.transform = CGAffineTransformMakeScale(0.1, 0.1) 
    UIView.animateWithDuration(1.0, animations: { () -> Void in
            cell.transform = CGAffineTransformIdentity
        }, completion: nil)

return cell
}

您可能希望将动画应用到 cell.messageImage。

================================================ ============================ 对不起,我完全误解了你的问题。我以为您想要从 CGRectZero 出现的单元格动画。我认为当您在 tabbarcontroller 或 navigationviewcontroller 中有 viewcontrollr 时,您不会在 viewDidLoad() 中获得正确的布局。您应该将 autoresizingMask 设置为 imageCollectionViewController.view 或在 viewWillAppear() 中添加您的 childviewcotroller 或执行以下操作。

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    self.imageCollectionViewController!.view.frame = self.containerView.bounds
}

【讨论】:

  • 有趣的是,如果我删除 tabbarhidden 行,单元格会正常显示。
猜你喜欢
  • 2016-04-10
  • 2011-01-25
  • 2012-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多