gpt4 book ai didi

swift - CollectionView selectItem TintColor 未突出显示或选择该项目

转载 作者:行者123 更新时间:2023-11-30 12:23:31 27 4
gpt4 key购买 nike

我在MenuClass中有这个 Collection View 如下:

class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.rgb(colorLiteralRed: 230, green: 32, blue: 31, alpha: 1)
cv.dataSource = self
cv.delegate = self
return cv
}()

override init(frame: CGRect) {
super.init(frame: frame)

addSubview(collectionView)
collectionView.register(MenuCell.self, forCellWithReuseIdentifier: cellId)
//NOT WORKING
let selectIndexPath = IndexPath(item: 0, section: 0)
collectionView.selectItem(at: selectIndexPath, animated: false, scrollPosition: [])

addConstraintsWithFormat(format: "H:|[v0]|", views: collectionView)
addConstraintsWithFormat(format: "V:|[v0]|", views: collectionView)

backgroundColor = UIColor.rgb(colorLiteralRed: 230, green: 32, blue: 31, alpha: 1)
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MenuCell
cell.imageView.image = UIImage(named: imageName[indexPath.row])?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
cell.imageView.tintColor = UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1)
return cell
}
}

这是MenuCell我为上面的 Collection View 注册的类:

class MenuCell: BaseCell {

let imageView: UIImageView = {
let iv = UIImageView()
return iv
}()


override var isHighlighted: Bool {
didSet {
imageView.tintColor = isHighlighted ? UIColor.white : UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1)
}
}

override var isSelected: Bool {
didSet {
imageView.tintColor = isSelected ? UIColor.white : UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1)
}
}

override func setUpViews() {
super.setUpViews()
addSubview(imageView)
addConstraintsWithFormat(format: "H:[v0(28)]", views: imageView)
addConstraintsWithFormat(format: "V:[v0(28)]", views: imageView)

addConstraint(NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: imageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
}
}

我的问题是在 MenuBar init 内这些代码行中的类方法:

let selectIndexPath = IndexPath(item: 0, section: 0)
collectionView.selectItem(at: selectIndexPath, animated: false, scrollPosition: [])

它没有选择 Collection View 的第一项并将其突出显示为白色作为默认选定的项目。我认为这与这一行有关:cell.imageView.tintColor = UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1)里面cellForItemAt MenuBar中的方法以及 isSelected 内的覆盖属性( isHighLightedMenuCell ) .

我不确定这种着色如何在 imageView 上发挥作用。 (我在 MenuCell 上定义)或者我是否正确使用了属性。有人可以让我知道如何使用着色和属性来实现这一目标。主页按钮突出显示为白色:

enter image description here

最佳答案

好的,我在 MenuBar 的 cellForItemAt 方法中更改了这一行:

cell.imageView.image = UIImage(命名: imageName[indexPath.row])?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)

至:

cell.image = UIImage(命名: imageName[indexPath.row])?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)

通过删除该 imageView 并由于某种原因它修复了问题并现在默认选择第一项。谁能告诉我为什么这有效?

关于swift - CollectionView selectItem TintColor 未突出显示或选择该项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44508393/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com