UITableViewCell accessory not being tinted

前端 未结 2 647
礼貌的吻别
礼貌的吻别 2021-01-04 10:52

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

相关标签:
2条回答
  • 2021-01-04 11:28

    Here is what helped me and should help others.

    The tintColor attribute of any UIView type and subtypes will propagate it's tint setting to the subviews in it's hierarchy. You could set the tintColor for a UITableView and it would be applied to all the cells within.

    That said not all UITableViewCell Accessory types can unfortunately be tinted.

    Those who get tinted:

    • UITableViewCellAccessoryCheckmark
    • UITableViewCellAccessoryDetailButton
    • UITableViewCellAccessoryDetailDisclosureButton -> only the Details part

    The following do not get tinted:

    • UITableViewCellAccessoryDisclosureIndicator
    • UITableViewCellAccessoryDetailDisclosureButton -> only the Disclosure Button

    So generally, you'll be able to change the color of your UITableViewCell Accessories. But if you want to change the grey arrow that usually indicates a segue to another view, no chance, it will stay grey.

    The only way to change it is to actually create a custom UIAccessoryView. Here is one implementation purposefully broken down to keep it clear. Although I believe better ways exist:

    In my custom UITableViewCell Class in the awakeFromNib() method

    let disclosureImage = UIImage(named: "Disclosure Image")
    let disclosureView = UIImageView(image: disclosureImage)
    disclosureView.frame = CGRectMake(0, 0, 25, 25)
    self.accessoryView = disclosureView
    

    Be warned that this cannot be tinted either. It will have the color of the image used compared to "Tab Bar Items" so you might need multiple images for selected cells and unselected ones.

    0 讨论(0)
  • 2021-01-04 11:38

    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];
        }
    }
    
    0 讨论(0)
提交回复
热议问题