Change color of accessoryType Swift

2020-08-09 07:03发布

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

11条回答
祖国的老花朵
2楼-- · 2020-08-09 07:20

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

查看更多
神经病院院长
3楼-- · 2020-08-09 07:22

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
查看更多
一纸荒年 Trace。
4楼-- · 2020-08-09 07:24

"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:

enter image description here

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 }
   }
}

enter image description here

查看更多
三岁会撩人
5楼-- · 2020-08-09 07:29

cell.contentView.backgroundColor = .grey

cell.backgroundColor = .grey

查看更多
我欲成王,谁敢阻挡
6楼-- · 2020-08-09 07:30

Swift 3.1:

cell.backgroundColor = UIColor.yellow // the accessoryType background 
cell.tintColor = UIColor.black // the accessoryType tint color.
查看更多
女痞
7楼-- · 2020-08-09 07:30

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: enter image description here

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

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
            }
        }

    }
}
查看更多
登录 后发表回答