Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - How to change the color of an accessoryType (disclosureIndicator)?

I have question about the accessoryType of cells. I am using a cell with an disclosureIndicator as accessoryType and I want to change it's color but I can't. Does anyone know if this is a bug or if Apple forces me to use the grey color? Actually I can change the colors of other accessoryType.

My code looks like this:

let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! customCell
cell.tintColor = UIColor.red
cell.accessoryType = .disclosureIndicator

And my arrow is still grey. But if I use a checkmark accessoryType it becomes red.

Is there any way to fix this or do I have to use a colored image?

like image 968
Godlike Avatar asked Jul 05 '17 13:07

Godlike


3 Answers

You can do something like this

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.tintColor = UIColor.white

    let image = UIImage(named: "Arrow.png")
    let checkmark  = UIImageView(frame:CGRect(x:0, y:0, width:(image?.size.width)!, height:(image?.size.height)!));
    checkmark.image = image
    cell.accessoryView = checkmark

    let object = objects[indexPath.row] as! NSDate
    cell.textLabel!.text = object.description
    return cell
}

Sample Arrow Images

enter image description here enter image description here

Output

enter image description here

like image 195
Bhavesh Dhaduk Avatar answered Oct 04 '22 17:10

Bhavesh Dhaduk


Updated for Swift 4.2 with images attached:

    cell.accessoryType = .disclosureIndicator
    cell.tintColor = .black
    let image = UIImage(named:"disclosureArrow")?.withRenderingMode(.alwaysTemplate)
    if let width = image?.size.width, let height = image?.size.height {
        let disclosureImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
        disclosureImageView.image = image
        cell.accessoryView = disclosureImageView
    }

Images you can use:

1x2x3x

What it could look like:

example

like image 33
Joshua Hart Avatar answered Oct 04 '22 16:10

Joshua Hart


Use SF Symbol

let image = UIImage(systemName: "chevron.right")
let accessory  = UIImageView(frame:CGRect(x:0, y:0, width:(image?.size.width)!, height:(image?.size.height)!))
accessory.image = image

// set the color here
accessory.tintColor = UIColor.white
cell.accessoryView = accessory
like image 36
Abhishek Sinha Avatar answered Oct 04 '22 15:10

Abhishek Sinha