how can i change the color of collectionview cells depending on the display theme based on my color scheme?

A very kind guy helped me out and found this solution. The problem here is that I forgot the view’s hierarchy.

CollectionView cell

 override func layoutSubviews() {
        super.layoutSubviews()
           setupBackGround()
    } 
func setupBackGround(){
backgroundColor = isHighlighted ? highlighColor : defaultColor
}

KeyboardViewController


func setColorScheme(_ colorScheme: ColorScheme) {
      let colorScheme = CColors(colorScheme: colorScheme)
     
      for view in subviews {
         func setToRootView(view: UIView) {
                if let cell = view as? KeyboardKeys {
                    cell.tintColor = colorScheme.buttonTextColor
                    cell.defaultColor = colorScheme.keysDefaultColor
                    cell.highlighColor = colorScheme.keysHighlightColor
                    cell.setBackground()
                    return
                }
                guard view.subviews.count > 0 else {
                    return
                }
                view.subviews.forEach(setToRootView(view:))
            }
            setToRootView(view: self)
        }

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top