首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在我的CollectionViewCell中添加阴影和圆角?

如何在我的CollectionViewCell中添加阴影和圆角?
EN

Stack Overflow用户
提问于 2021-12-23 18:02:02
回答 1查看 253关注 0票数 1

我对iOS编程还是很陌生的,但是为了磨练我的技能,我正在尝试开发一个小应用程序。现在,我有一些问题,添加一些阴影和圆角到我的CollectionViewCell。

我有“主ViewController”,我只为CollectionView单元创建了一个单独的文件,我称之为"MyCustomCollectionViewCell“,我将在这里粘贴它,然后解释问题的所在。

"Main ViewController":

代码语言:javascript
运行
复制
    import UIKit

class ViewController: UIViewController {
   
  //OUTLETS
   
  @IBOutlet weak var UICollectionView: UICollectionView!
   
  //VARIABLES & CONSTANTS
   
  private let myNames = ["Jesus Urbano", "Juan Antonio", "Diego Armando", "Alejanda Almaguer", "Alma Delia", "Alma Cecilia", "Cristian Rodriguez", "Rene Canales", "Monica Canales", "Angel Canales", "Angel Acevedo", "Claudia Mondragon", "Karen Mondragon", "Efrain Almaguer", "Ester Rivera", "Arleth Almaguer"]
   
  override func viewDidLoad() {
    super.viewDidLoad()
     
    UICollectionView.dataSource = self
     
    UICollectionView.register(UINib(nibName: "MyCustomCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "mycell")
     
  }

}


// MARK: - UICollectionViewDataSource

extension ViewController: UICollectionViewDataSource {
   
  func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
  }

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
     
    return myNames.count
  }

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
   
  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mycell", for: indexPath) as? MyCustomCollectionViewCell
     
  cell?.layer.cornerRadius = 8
  cell?.layer.masksToBounds = false
  cell?.layer.shadowColor = UIColor.black.cgColor
  cell?.layer.shadowOffset = CGSize(width: 5.0, height: 5.0)
  cell?.layer.shadowOpacity = 0.5
  cell?.layer.shadowRadius = 1.0

     
  cell?.myOcupationLabel.text = "Albañil"
  cell?.mySpecialtyLabel.text = "Pisos y Acabados"
  cell?.myNameLabel.text = myNames[indexPath.row]

    return cell!

"MyCustomCollectionViewCell":

代码语言:javascript
运行
复制
import UIKit

class MyCustomCollectionViewCell: UICollectionViewCell {
   
  //IMAGENES
   
  @IBOutlet weak var myImage: UIImageView!
   
  //ETIQUETAS
   
  @IBOutlet weak var myOcupationLabel: UILabel!
  @IBOutlet weak var mySpecialtyLabel: UILabel!
  @IBOutlet weak var myNameLabel: UILabel!
  
  //BOTONES
  @IBOutlet weak var myContactButton: UIButton!
   
   
  override func awakeFromNib() {
    super.awakeFromNib()
     
     
    //PROPIEDADES
       
    // Etiqueta de Nombre
    
    myNameLabel.font = UIFont.boldSystemFont(ofSize: 15)
     
    // Boton de Contacto
     
    myContactButton.layer.cornerRadius = 18
    myContactButton.layer.masksToBounds = false
    clipsToBounds = true
     
    myContactButton.layer.shadowColor = UIColor.darkGray.cgColor
    myContactButton.layer.shadowRadius = 3
    myContactButton.layer.shadowOpacity = 1.0
    myContactButton.layer.shadowOffset = CGSize(width: 0, height: 3)
     
  }

}

正如我前面提到的,我试图绕过我的CollectionView单元格的角落,并给它添加一些阴影。问题是,每次我都可以得到圆角或阴影,但不能同时进行。

当我只使用这一行代码时,我可以得到我想要的角。

cell?.layer.cornerRadius =8

另一方面,如果我使用整个街区,我只得到阴影,没有圆角。

代码语言:javascript
运行
复制
 cell?.layer.cornerRadius = 8
 cell?.layer.masksToBounds = false
 cell?.layer.shadowColor = UIColor.black.cgColor
 cell?.layer.shadowOffset = CGSize(width: 5.0, height: 5.0)
 cell?.layer.shadowOpacity = 0.5
 cell?.layer.shadowRadius = 1.0

你们知道为什么会这样吗?

提前谢谢你!

EN

Stack Overflow用户

发布于 2021-12-23 18:15:33

而不是试图绕过UICollectionViewCell self的角落,但是您需要更改单元格的contentView属性。确保检查单元格的backgroundColorcontentView

您可以这样做(taken from here):

代码语言:javascript
运行
复制
contentView.layer.cornerRadius = 8.0
contentView.layer.borderWidth = 1.0
contentView.layer.borderColor = UIColor.clear.cgColor
contentView.layer.masksToBounds = true

代码语言:javascript
运行
复制
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 5.0, height: 5.0)
layer.shadowRadius = 1.0
layer.shadowOpacity = 0.5
layer.masksToBounds = false
layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: contentView.layer.cornerRadius).cgPath
layer.backgroundColor = UIColor.clear.cgColor
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70465844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档