Swift CollectionView selectItem tintColor未高亮显示或选择项目

Swift CollectionView selectItem tintColor未高亮显示或选择项目,swift,Swift,我在下面的菜单类中有此集合视图: class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { lazy var collectionView: UICollectionView = { let layout = UICollectionViewFlowLayout() let cv = UI

我在下面的
菜单类中有此集合视图:

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
类:

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: [])
它不是选择集合视图的第一个项目并将其高亮显示为白色作为默认的选定项目。我认为这与以下行有关:
cell.imageView.tintColor=UIColor.rgb(colorLiteralRed:91,green:14,blue:13,alpha:1)
MenuBar
中的
cellForItemAt
方法中,以及
MenuCell
中被重写的属性(
isSelected
isHighLighted

我不确定此着色在
imageView
(我在
MenuCell
上定义)上是如何工作的,或者我是否正确使用了属性。有人能让我知道如何使用着色和属性来实现这一点。主页按钮高亮显示为白色时:


好,所以我在菜单栏中的
cellForItemAt
方法中更改了这一行:

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

致:

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


通过删除
imageView
,出于某种原因,它修复了问题,并默认选择了第一项。有人能告诉我为什么这样做吗?

错误是因为您正在为图像视图设置色彩,而不是集合视图(…,cellForItemAt…)中菜单栏类中的单元格本身

//走错了路

cell.imageView.tintColor = UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1)
cell.tintColor = UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1)
将“染色颜色”设置为单元格,这样就可以了

//正确的方法

cell.imageView.tintColor = UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1)
cell.tintColor = UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1)