UITableViewCell accessory not being tinted

Tokuriku picture Tokuriku · Jan 15, 2015 · Viewed 8.7k times · Source

I've read multiple Q&A on the topic but none seem to work so here's my problem.

I created a custom UITableViewCell and in the Storyboard, I asked for there to be a disclosure indicator accessory. It is said that the tintColor should change the color of the indicator but after much research, here is what I found:

  • The cell's accessoryView maps out to a nil value therefore I cannot affect it's tintColor

I tried to create the accessoryView as done with a selectedBackgroundView like such:

self.accessoryView = UIView()

Obviously, it just creates a blank space and the original disclosure accessory disappears. I'm really confused with all this and cannot find a way to affect the color of a cell's accessory. Any help would be much welcome!

Answer

Zelko picture Zelko · Mar 22, 2016

Extension

extension UITableViewCell {
    func prepareDisclosureIndicator() {
        for case let button as UIButton in subviews {
            let image = button.backgroundImageForState(.Normal)?.imageWithRenderingMode(.AlwaysTemplate)
            button.setBackgroundImage(image, forState: .Normal)
        }
    }
}

Swift 3 :

    extension UITableViewCell {
        func prepareDisclosureIndicator() {
            for case let button as UIButton in subviews {
            let image = button.backgroundImage(for: .normal)?.withRenderingMode(.
                alwaysTemplate)
            button.setBackgroundImage(image, for: .normal)
        }
    }
}

Do it

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.prepareDisclosureIndicator()
}

swift 3 :

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.prepareDisclosureIndicator()
    
}

Objective-C:

for (UIView *subview in cell.subviews) {
    if ([subview isMemberOfClass:[UIButton class]]) {
        UIButton *button = (UIButton *)subview;
        UIImage *image = [[button backgroundImageForState:UIControlStateNormal] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [button setBackgroundImage:image forState:UIControlStateNormal];
    }
}