【问题标题】:Proper Timing to Draw Circles using Autolayout inside UICollectionView在 UICollectionView 中使用 Autolayout 绘制圆圈的正确时机
【发布时间】:2018-08-29 12:49:31
【问题描述】:

在每个集合视图单元格中,我都有一个视图,我想制作一个完美的圆圈。我使用 Autolayout 将单元格的宽度基于主视图的宽度。

我的问题:有些视图没有一个完美的圆圈。我猜这是时间上的冲突,在将这些 UIView 变成圆圈之前,我还没有设置主视图边界。我应该在特定时间绘制cornerRadius吗?其他一切正常。谢谢!

func collectionView(_ collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAt: IndexPath) -> CGSize {
    return CGSize(width: self.view.frame.width/3, height: 400)
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let sessionCell = (collectionView.dequeueReusableCell(withReuseIdentifier: Storyboard.cellID, for: indexPath)) as? DayCollectionViewCell {
        sessionCell.circleView.layer.cornerRadius = sessionCell.circleView.frame.width/2
        return sessionCell
    }
}

【问题讨论】:

    标签: swift autolayout uicollectionviewcell draw uicollectionviewlayout


    【解决方案1】:

    你需要在这里设置

    override func layoutSubviews() {
      super.layoutSubviews()
      self.circleView.layer.cornerRadius = self.circleView.frame.width/2
    }
    

    DayCollectionViewCell 类内

    【讨论】:

    • 太棒了,就是这样。谢谢!
    猜你喜欢
    • 2023-01-13
    • 2021-12-16
    • 2020-04-17
    • 2019-01-17
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    相关资源
    最近更新 更多