我在下面的MenuClass
中有这个集合视图:
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)
cellForItemAt
方法MenuBar
内的isSelected
以及isHighLighted
和MenuCell
内的重写属性(imageView
和MenuCell
) 1}}。
我不确定这种着色是如何在Uncaught (in promise) TypeError: Cannot read property 'call' of undefined
(我在/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__base_base_icon__ = __webpack_require__(567);
上定义)或我是否正确使用这些属性的。有人可以让我知道如何使用着色和属性来实现这一目标。主页按钮突出显示为白色:
答案 0 :(得分:0)
好的,所以我在MenuBar中的cellForItemAt
方法中更改了这一行:
cell.imageView.image = UIImage(named: imageName[indexPath.row])?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
为:
cell.image = UIImage(named: imageName[indexPath.row])?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
删除imageView
并因某种原因修复了问题,现在默认选择第一项。任何人都可以让我知道为什么这有用吗?
答案 1 :(得分:0)
错误是因为您在内的MenuBar类中为 imageView 而不是单元格设置 tintColor collectionView(...,cellForItemAt ...)
//错误的方式
cell.imageView.tintColor = UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1)
将tintColor设置为单元格,它将执行
//正确的方式
cell.tintColor = UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1)