Change color of accessoryType Swift

2020-08-09 07:07发布

问题:

I'd like to change the color of my cell accessoryType from blue to white. The textColor is already set to white. Does someone of you guys know how to do this?

My Code:

cell!.accessoryType = UITableViewCellAccessoryType.Checkmark

回答1:

You can set your UITableViewCell tintColor property to the desired color:

[cell setTintColor:[UIColor whiteColor]];


回答2:

Swift:

cell.tintColor = UIColor.whiteColor()

Swift 3.0

cell.tintColor = UIColor.white


回答3:

Doesn't work for .disclosureIndicator ?

If someone is here looking for "How to change color for .disclosureIndicator indicator type".

The answer is you can't. BUT:

There is a way. Apply a custom image:

let chevronImageView = UIImageView(image: "disclosureIndicatorImage"))
accessoryView = chevronImageView

Additional supporting links:

The image can be downloaded from here. How to change color of the image is here.



回答4:

The best way to do that, i think, is to set accessory to image the following way:

let image = UIImage(named: "some image.png")
cell.accessoryView = image


回答5:

Swift 3.1:

cell.backgroundColor = UIColor.yellow // the accessoryType background 
cell.tintColor = UIColor.black // the accessoryType tint color.


回答6:

"How to change color for .disclosureIndicator indicator type". After much research, I noticed that the image of disclosureIndicator is not an Image but a backgroundImage. I found a solution like this:

import UIKit

class CustomCell: UITableViewCell {


fileprivate func commonInit() {

}

open override func layoutSubviews() {
    super.layoutSubviews()

    if let indicatorButton = allSubviews.compactMap({ $0 as? UIButton }).last {
        let image = indicatorButton.backgroundImage(for: .normal)?.withRenderingMode(.alwaysTemplate)
        indicatorButton.setBackgroundImage(image, for: .normal)
        indicatorButton.tintColor = .red
     }
  }
}

extension UIView {
   var allSubviews: [UIView] {
      return subviews.flatMap { [$0] + $0.allSubviews }
   }
}



回答7:

Swift 5 I am Improving Tung Fam Answer

let chevronImageView = UIImageView(image: UIImage(named: "chevron-right"))
cell.accessoryView = chevronImageView


回答8:

You can also change the cell's tint color from the storyboard (assuming you are using a xib file)



回答9:

Im my case i need to change contentView color of my CustomCell.

Its can be easy making when u override methods :

override func setHighlighted(highlighted: Bool, animated: Bool) {}

and:

override func setSelected(selected: Bool, animated: Bool) {}

But when i add to my customCell :

cell?.accessoryType = .DisclosureIndicator

i had a problem when view under DisclosureIndicator is not change color. Its looks like:

So i look on subviews of CustomCell and find that DisclosureIndicator is a button. If change background color of this button u have this

So i try to change background color of superview of this button. And its work great.

Full code of myCustomCell setHighlighted func :

override func setHighlighted(highlighted: Bool, animated: Bool) {
    if(highlighted){
        viewContent.view.backgroundColor = Constants.Colors.selectedBackground
        for item in self.subviews {
            if ((item as? UIButton) != nil) {
                item.superview?.backgroundColor = Constants.Colors.selectedBackground
            }
        }

    } else {
        viewContent.view.backgroundColor = Constants.Colors.normalCellBackground
        for item in self.subviews {
            if ((item as? UIButton) != nil) {
                item.superview?.backgroundColor = Constants.Colors.normalCellBackground
            }
        }

    }
}


回答10:

If you are targeting iOS 13+ you can use the SF symbols right chevron (it's what Apple uses)

let chevronImageView = UIImageView(image: UIImage(systemName: "chevron.right", withConfiguration: UIImage.SymbolConfiguration(weight: .medium))) 
chevronImageView.tintColor = // your color                                
cell.accessoryView = chevronImageView


回答11:

cell.contentView.backgroundColor = .grey

cell.backgroundColor = .grey