UICollectionViewCell layoutSubview Вызывается, но не работает - PullRequest
0 голосов
/ 17 марта 2020

Я создаю подклассы для ячеек UICollectionView, чтобы обеспечить равномерное округление в нескольких коллекциях. Он работает на первом представлении коллекции, но не на втором (отдельный V C). Что мне не хватает? Спасибо!

Вот что я пытаюсь подкласс:

class RoundedCollectionViewCell: UICollectionViewCell {

    override func layoutSubviews() {

    self.layer.cornerRadius = 20.0
    self.layer.masksToBounds = true

    self.contentView.layer.cornerRadius = 15.0
    self.contentView.layer.borderWidth = 5.0
    let borderColor: UIColor = .clear
    self.contentView.layer.borderColor = borderColor.cgColor
    self.contentView.layer.masksToBounds = true
    self.layer.shadowColor = UIColor.white.cgColor
    self.layer.shadowOffset = CGSize(width: 0, height: 0.0)
    self.layer.shadowRadius = 2.0
    self.layer.shadowOpacity = 0.6
    self.layer.cornerRadius = 2.0
    self.layer.masksToBounds = false
    self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.contentView.layer.cornerRadius).cgPath

    print("RoundedCollectionViewCell called") //This prints correctly

}
}

Вот V C, который не округляет ячейки:

import UIKit

class PeopleViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

var persons : [Person] = []

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

@IBOutlet weak var peopleCollectionView: UICollectionView!

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PeopleCell", for: indexPath) as! PeopleCollectionViewCell

    cell.label.text = persons[indexPath.row].name

    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let noOfCellsInRow = 2
    let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
    let totalSpace = flowLayout.sectionInset.left
        + flowLayout.sectionInset.right
        + (flowLayout.minimumInteritemSpacing * CGFloat(noOfCellsInRow - 1))
    let size = Int((collectionView.bounds.width - totalSpace) / CGFloat(noOfCellsInRow))
    return CGSize(width: size, height: size)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

     if let cell = sender as? UICollectionViewCell,
        let indexPath = self.peopleCollectionView.indexPath(for: cell) {
        let vc = segue.destination as! PersonViewController
        vc.personToHighlight = persons[indexPath.row] //Pass specific person
     }
}

override func viewDidLoad() {
    super.viewDidLoad()
}

}

И, наконец, Сама ячейка:

import UIKit

class PeopleCollectionViewCell: RoundedCollectionViewCell {

@IBOutlet weak var label: UILabel!

}
...