内容上方的 UICollectionViewCell 的 contentView 背景层
UICollectionViewCell's contentView background layers over the content
我有一个使用 UICollectionViewCell 的 collectionView
collectionView.register(CustomCell.self, forCellWithReuseIdentifier: CustomCell.identifier)
我正在使用 isSelected 变量来更改所选单元格的背景。
class CategoryCell: UICollectionViewCell {
override var isSelected: Bool {
didSet {
if self.isSelected {
self.contentView.backgroundColor = UIColor(hexString: "#f0f0f0")
} else {
self.contentView.backgroundColor = UIColor.clear
}
}
}
但是背景颜色会像下面这样应用于单元格的内容。
在 ios 13 上不是这样的。我认为它是从 ios 14 或 15 开始发生的。知道吗?提前谢谢你。
编辑:
class CategoryCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
self.backgroundColor = UIColor.white
}
let myImage: UIImageView = {
let image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
return image
}()
let myLabel: UILabel = {
let label = UILabel()
label.text = "Custom Text"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
label.minimumScaleFactor = 0.2
label.adjustsFontSizeToFitWidth = true
return label
}()
func setupViews() {
addSubview(myImage)
addSubview(myLavel)
/*setup auto layout*/
}
static var identifier: String {
return String(describing: self)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var isSelected: Bool {
didSet {
if self.isSelected {
self.contentView.backgroundColor = UIColor(hexString: "#f0f0f0")
} else {
self.contentView.backgroundColor = UIColor.clear
}
}
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let customCell = collectionView.dequeueReusableCell(withReuseIdentifier: CustomCell.identifier, for: indexPath) as! CustomCell
customCell.myLabel.text = "Pepe"
customCell.myImage.image = UIImage(named: "pepe")
return categoryCell
}
按照 Fahim Parkar 的说法,以下解决了这个问题。
contentView.addSubview(myImage)
contentView.addSubview(myLavel)
我有一个使用 UICollectionViewCell 的 collectionView
collectionView.register(CustomCell.self, forCellWithReuseIdentifier: CustomCell.identifier)
我正在使用 isSelected 变量来更改所选单元格的背景。
class CategoryCell: UICollectionViewCell {
override var isSelected: Bool {
didSet {
if self.isSelected {
self.contentView.backgroundColor = UIColor(hexString: "#f0f0f0")
} else {
self.contentView.backgroundColor = UIColor.clear
}
}
}
但是背景颜色会像下面这样应用于单元格的内容。
在 ios 13 上不是这样的。我认为它是从 ios 14 或 15 开始发生的。知道吗?提前谢谢你。
编辑:
class CategoryCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
self.backgroundColor = UIColor.white
}
let myImage: UIImageView = {
let image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
return image
}()
let myLabel: UILabel = {
let label = UILabel()
label.text = "Custom Text"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
label.minimumScaleFactor = 0.2
label.adjustsFontSizeToFitWidth = true
return label
}()
func setupViews() {
addSubview(myImage)
addSubview(myLavel)
/*setup auto layout*/
}
static var identifier: String {
return String(describing: self)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var isSelected: Bool {
didSet {
if self.isSelected {
self.contentView.backgroundColor = UIColor(hexString: "#f0f0f0")
} else {
self.contentView.backgroundColor = UIColor.clear
}
}
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let customCell = collectionView.dequeueReusableCell(withReuseIdentifier: CustomCell.identifier, for: indexPath) as! CustomCell
customCell.myLabel.text = "Pepe"
customCell.myImage.image = UIImage(named: "pepe")
return categoryCell
}
按照 Fahim Parkar 的说法,以下解决了这个问题。
contentView.addSubview(myImage)
contentView.addSubview(myLavel)