UICollectionViewCell alternative selectionStyle = .none

huangapple go评论64阅读模式
英文:

UICollectionViewCell alternative selectionStyle = .none

问题

UICollectionViewCell的替代属性(tableViewCell中的selectionStyle)是什么?如何禁用默认的选中灰色颜色?

英文:

What is the alternative property (selectionStyle in tableViewCell) for UICollectionViewCell ? How to disable default selection gray color ?

答案1

得分: 1

第一种方法,尝试使用didDeselect方法将contentView的背景颜色更改为clear

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath) as! YourCell
    cell.contentView.backgroundColor = .clear
}

第二种方法,在你的collectionViewCell中重写isSelected属性:

class YourCollectionViewCell: UICollectionViewCell {
    
    override var isSelected: Bool {
        didSet {
            self.contentView.backgroundColor = isSelected ? UIColor.yourColor : UIColor.yourColor // 在这里设置你的颜色
        }
    }
    // 这里是你的其他UICollectionViewCell代码
}
英文:

First method, Try to change contentView background color to clear with didSelect method:

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
 let cell = collectionview.cellForItem(at: indexPath) as! YourCell
 cell.contentView.backgroundColor = .clear
}

Second method isSelected, in your collectionViewCell override isSelected var :

class YourCollectionViewCell: UICollectionViewCell {

override var isSelected: Bool {
    didSet {
        self.contentView.backgroundColor = isSelected ? UIColor.yourColor : UIColor.yourColor // set your colors here
    }
}
// here the rest of your UICollectionViewCell code

huangapple
  • 本文由 发表于 2023年3月7日 22:14:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定