【发布时间】:2021-03-06 15:55:03
【问题描述】:
在我的应用程序(键盘扩展)中,我设置了一个配色方案以根据主题(浅色/深色)更改背景颜色。
我创建了一个集合视图,并使用我的配色方案设置了它的颜色。但是,如果集合视图是可滚动的,则滚动后某些按钮的颜色会发生变化。
如何防止它发生?
这只发生在暗模式下
这就是我设置配色方案的方式
enum Scheme {
case dark
case light
}
struct Colors {
let keysDefaultColor: UIColor
let keysHighlightColor: UIColor
let grayKeysDefaultColor: UIColor
let grayKeysHighlightColor: UIColor
let buttonTextColor: UIColor
init(colorScheme: Scheme) {
switch colorScheme {
case .light:
keysDefaultColor = UIColor.white
keysHighlightColor = UIColor.lightGray.withAlphaComponent(0.6)
grayKeysDefaultColor = UIColor.lightGray.withAlphaComponent(0.6)
grayKeysHighlightColor = UIColor.white
buttonTextColor = .black
case .dark:
keysDefaultColor = UIColor.gray.withAlphaComponent(0.5)
keysHighlightColor = UIColor.lightGray.withAlphaComponent(0.5)
grayKeysDefaultColor = UIColor.darkGray.withAlphaComponent(0.5)
grayKeysHighlightColor = UIColor.gray.withAlphaComponent(0.5)
buttonTextColor = .white
}
}
}
然后我有一个集合视图,我为单元格创建了一个自定义类。在我声明并设置集合视图(可滚动)之后,我创建了以下函数来设置它的颜色:
func setColorScheme(_ colorScheme: Scheme) {
let colorScheme = Colors(colorScheme: colorScheme)
func setToRootView(view: UIView) {
if let cell = view as? CustomCells {
cell.label.textColor = colorScheme.buttonTextColor
cell.defaultColor = colorScheme.keysDefaultColor
cell.highlighColor = colorScheme.keysHighlightColor
cell.setBackground() //This sets highlight background on tap and default for normal state
return
}
guard view.subviews.count > 0 else {
return
}
view.subviews.forEach(setToRootView(view:))
}
setToRootView(view: self)
}
我在视图的初始化中调用了这个函数,我在这里放置了集合视图和键盘视图控制器:
override func textDidChange(_ textInput: UITextInput?) {
// The app has just changed the document's contents, the document context has been updated.
let colorScheme: Scheme
let proxy = self.textDocumentProxy
if proxy.keyboardAppearance == UIKeyboardAppearance.dark {
colorScheme = .dark
} else {
colorScheme = .light
}
myView.setColorScheme(colorScheme)
}
项目所在的单元格:
let cell = myCollection.dequeueReusableCell(withReuseIdentifier: "keyboardCellsId", for: indexPath) as! CustomCells
cell.label.text = String("abc")
return cell
所以我想我错过了一些东西。我知道我没有发布完整的代码,但这是因为我不想让问题变得太重,如果您需要更多,请告诉我。
【问题讨论】:
标签: ios swift view scroll uicollectionview