CollectionView 单元格滚动时背景发生变化
CollectionView cells Background changs when scrolling
在我的应用程序(键盘扩展)中,我设置了一个配色方案以根据主题更改背景颜色 (light/dark)。
我创建了一个集合视图,并使用我的配色方案设置了它的颜色。但是,如果集合视图是可滚动的,则滚动后某些按钮的颜色会发生变化。
我该如何防止它发生?
这只发生在黑暗模式
这就是我设置配色方案的方式
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
}
}
}
然后我有一个集合视图,对于我自定义的单元格 class。在声明并设置集合视图(可滚动)之后,我创建了以下函数来设置其颜色:
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
所以我想我遗漏了什么。我知道我没有 post 完整的代码,但这是因为我不想让这个问题太重,如果您需要更多,请告诉我。
能否分享一下您的 collectionView cellForItem
委托方法?
我认为您可能必须在 collectionView cellForItem
委托方法中设置单元格和按钮的颜色
它第一次正确加载的原因是因为所有可见单元格都设置为正确的颜色,但是在显示新单元格时按钮上的颜色可能设置不正确
在我的应用程序(键盘扩展)中,我设置了一个配色方案以根据主题更改背景颜色 (light/dark)。
我创建了一个集合视图,并使用我的配色方案设置了它的颜色。但是,如果集合视图是可滚动的,则滚动后某些按钮的颜色会发生变化。
我该如何防止它发生?
这只发生在黑暗模式
这就是我设置配色方案的方式
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
}
}
}
然后我有一个集合视图,对于我自定义的单元格 class。在声明并设置集合视图(可滚动)之后,我创建了以下函数来设置其颜色:
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
所以我想我遗漏了什么。我知道我没有 post 完整的代码,但这是因为我不想让这个问题太重,如果您需要更多,请告诉我。
能否分享一下您的 collectionView cellForItem
委托方法?
我认为您可能必须在 collectionView cellForItem
委托方法中设置单元格和按钮的颜色
它第一次正确加载的原因是因为所有可见单元格都设置为正确的颜色,但是在显示新单元格时按钮上的颜色可能设置不正确