加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – Swift:如何将自定义UICollectionViewCell设置为圆?

发布时间:2020-12-14 17:17:11 所属栏目:百科 来源:网络整理
导读:我定制了包含UILabel和UI Image的UICollectionViewCell. 我想以圆形格式设置单元格. 注意: – 由于AutoLayout,无法更改sizeForItemAtIndexPath. 以下是我用过的代码: func collectionView(collectionView: UICollectionView,layout collectionViewLayout:
我定制了包含UILabel和UI Image的UICollectionViewCell.
我想以圆形格式设置单元格.

注意: – 由于AutoLayout,无法更改sizeForItemAtIndexPath.

以下是我用过的代码:

func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath ) -> CGSize {

          return CGSize(width: (self.objKeypadCollectionView.frame.size.width/3)-10,height: (self.objKeypadCollectionView.frame.size.height/4)-10)
    }

func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let objKeypadCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("idKeypadCollectionViewCell",forIndexPath: indexPath) as! KeypadCollectionViewCell

        if (self.numberArray[indexPath.item])=="asda" {
            objKeypadCollectionViewCell.lblNumber.text = ""
            objKeypadCollectionViewCell.imgPrint.hidden = false
        }
        else if (self.numberArray[indexPath.item])=="Derterel" {
            objKeypadCollectionViewCell.lblNumber.text = self.numberArray[indexPath.item]
            objKeypadCollectionViewCell.imgPrint.hidden = true
        }
        else {
            objKeypadCollectionViewCell.imgPrint.hidden = true
            objKeypadCollectionViewCell.lblNumber.text = self.numberArray[indexPath.item]
            objKeypadCollectionViewCell.layer.borderColor = UIColor.lightGrayColor().CGColor
            objKeypadCollectionViewCell.layer.borderWidth = 1
            objKeypadCollectionViewCell.layer.cornerRadius = objKeypadCollectionViewCell.frame.size.width/2
        }
        return objKeypadCollectionViewCell
    }

Screenshot 1

Design screen

解决方法

这是我的UICollectionView示例,您可以检查您的解决方案.我正在使用storyboard来设置带有AutoLayout的主集合.我也为这些东西附上了一些截图.

有一个名为drawRect的方法.您可以在collectionView Cell类中使用它来执行UI内容.

现在这是我的代码.

1. ViewController.swift //就像普通的ViewController一样,没有别的……

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读