- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Apple说:
Because color transformers can use the same base input color to produce a number of variants of that color, you can create different appearances for different states of your views.
好的。我想尝试使用新的 iOS 14 backgroundConfiguration
架构,而不是设置单元格的 backgroundView
和 selectedBackgroundView
。我希望我的 UITableViewCell 通常具有蓝色背景,但在被选中时具有灰色背景。所以,在我的 cellForRowAt
实现中:
var b = UIBackgroundConfiguration.listPlainCell()
b.backgroundColor = .blue
cell.backgroundConfiguration = b
到目前为止,还不错;细胞是蓝色的。但是灰色呢?这是我希望颜色更改器(mutator)发挥作用的地方:
var b = UIBackgroundConfiguration.listPlainCell()
b.backgroundColor = .blue
b.backgroundColorTransformer = // what?
cell.backgroundConfiguration = b
颜色更改器(mutator)是一个接受颜色并返回颜色的函数。通过记录,我可以看到每次单元格状态发生变化时,实际上都会调用颜色更改器(mutator)函数。但是函数本身不带状态。它无权访问单元格的状态。那么,我该如何像 Apple 声称的那样“为不同的状态创建不同的外观”呢?
最佳答案
据我所知,答案分为两部分:
您正在 cellForRowAt
中配置后台配置,因此您具有对单元格的引用,因此具有对单元格配置状态的引用。
如果您显式设置背景配置的backgroundColor
,则无法通过backgroundColorTransformer
进行设置。这是一个或另一个。 (这对我来说感觉有问题,但无论如何。)
所以,下面是我想要的:
var b = UIBackgroundConfiguration.listPlainCell()
b.backgroundColorTransformer = UIConfigurationColorTransformer { [weak cell] c in
if let state = cell?.configurationState {
if state.isSelected || state.isHighlighted {
return .gray
}
}
return .blue
}
cell.backgroundConfiguration = b
关于ios - UIConfigurationColorTransformer 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63960091/
Apple说: Because color transformers can use the same base input color to produce a number of variants
我是一名优秀的程序员,十分优秀!