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

前端之家收集整理的这篇文章主要介绍了ios – Swift:如何将自定义UICollectionViewCell设置为圆?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我定制了包含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一样,没有别的……

猜你在找的Xcode相关文章