Ios UICollectionViewCell layoutSubview已调用但不工作
我正在对UICollectionView单元格进行子类化,以便在多个CollectionView中具有统一的舍入。它适用于第一个collectionview,但不适用于第二个(单独的VC)。我错过了什么?谢谢 下面是我要尝试的子类:Ios UICollectionViewCell layoutSubview已调用但不工作,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我正在对UICollectionView单元格进行子类化,以便在多个CollectionView中具有统一的舍入。它适用于第一个collectionview,但不适用于第二个(单独的VC)。我错过了什么?谢谢 下面是我要尝试的子类: class RoundedCollectionViewCell: UICollectionViewCell { override func layoutSubviews() { self.layer.cornerRadius = 20.0
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
}
}
以下是未将单元格舍入的VC:
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!
}
使用awakefromnib方法代替layoutSubviews()尝试它;没有解决问题。