ios - Swift4,选择单元格时的背景颜色

标签 ios swift uikit

我试图在用户选择单元格时突出显示单元格的背景。 当用户选择单元格时,我已经成功地更改了单元格的边框。

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    addToList.append(objectsArray[indexPath.row])
    let cell = collectionView.cellForItem(at: indexPath)
    cell?.contentView.layer.borderWidth = 2.0
    cell?.contentView.layer.borderColor = UIColor.gray.cgColor
}

虽然我无法更改背景。

这能做到吗? 谢谢!

最佳答案

对于单元格边框,请像这样使用 cell contentView:

cell?.contentView.layer.borderWidth = 2.0
cell?.contentView.layer.borderColor = UIColor.gray.cgColor

对于单元格 backgroundColor 颜色,您需要使用:

cell?.backgroundColor = .gray

关于ios - Swift4,选择单元格时的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52276438/

相关文章:

macos - OSX中是否有等效于UIActivityViewController的东西?

ios - 在 tvOS 中重新加载数据后, Collection View 的最后一个聚焦索引路径丢失

ios - swift 启用使用 copy() 或 unshare method()

ios - NS条件 : wait for a specified amount of time

iphone - NSOperationQueue、内存和GCD;如何正确实现?

ios - iOS中的HeightForRowAt和estimatedHeightForRowAt

ios - 如何在 Swift 中访问 Json Int、Double、String 中的数据?

ios - cocoa : "perspective transform"可用吗?

ios - Swift Map Kit 在解包 Optional 值时意外发现 nil

ios - 当来自 NotificationCenter 时,为什么调整标签大小需要延迟才能按预期更新?