我已经阅读了有关该主题的多个问答,但似乎没有一个有效,所以这是我的问题。
我创建了一个自定义 UITableViewCell,并在故事板中,我要求有一个披露指示器附件。据说tintColor应该改变指示器的颜色,但经过大量研究,这是我发现的:
我尝试创建accessoryView,就像使用selectedBackgroundView一样:
self.accessoryView = UIView()
显然,它只是创建了一个空白空间,并且原来的披露配件消失了。我真的对这一切感到困惑,无法找到影响细胞配件颜色的方法。非常欢迎任何帮助!
extension UITableViewCell {
func prepareDisclosureIndicator() {
for case let button as UIButton in subviews {
let image = button.backgroundImageForState(.Normal)?.imageWithRenderingMode(.AlwaysTemplate)
button.setBackgroundImage(image, forState: .Normal)
}
}
}
斯威夫特 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)
}
}
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.prepareDisclosureIndicator()
}
迅捷3:
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.prepareDisclosureIndicator()
}
目标-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];
}
}
这对我有帮助,也应该帮助其他人。
任何 UIView 类型和子类型的tintColor 属性都会将其色调设置传播到其层次结构中的子视图。您可以为 UITableView 设置tintColor,它将应用于其中的所有单元格。
不幸的是,并非所有 UITableViewCell 附件类型都可以着色。
有色的人:
以下物品不会着色:
所以一般来说,您将能够更改 UITableViewCell Accessories 的颜色。但是,如果您想更改通常指示转至另一个视图的灰色箭头,那么没有机会,它将保持灰色。
更改它的唯一方法是实际创建一个自定义 UIAccessoryView。 这是一个为了保持清晰而特意分解的实现。 尽管我相信存在更好的方法:
在我的自定义 UITableViewCell 类中的 awakeFromNib() 方法中
let disclosureImage = UIImage(named: "Disclosure Image")
let disclosureView = UIImageView(image: disclosureImage)
disclosureView.frame = CGRectMake(0, 0, 25, 25)
self.accessoryView = disclosureView
请注意,这也不能着色。与“选项卡栏项目”相比,它将具有所使用的图像的颜色,因此您可能需要为选定的单元格和未选定的单元格提供多个图像。
这是 Tokuriku 对我们恐龙的答案的 Objective-C 版本:
UIImageSymbolConfiguration *configuration = [UIImageSymbolConfiguration configurationWithPointSize:15.0f weight:UIImageSymbolWeightUnspecified];
UIImageView *disclosureView = [[UIImageView alloc] initWithImage:[[UIImage systemImageNamed:@"chevron.right" withConfiguration:configuration] imageWithRenderingMode: UIImageRenderingModeAlwaysTemplate]];
disclosureView.frame = CGRectMake(0, 0, 15, 15);
disclosureView.tintColor = UIColor.systemYellowColor;
cell.accessoryView = disclosureView;
您可以使用名为“chevron.right”的系统映像来避免创建自己的映像。这里给出的帧大小似乎给出了接近原生 Apple 图像大小的图像。
对于您打算使用标准附件类型的其他单元,还要小心将 cell.accessoryView 设置为 nil
cell.accessoryType = self.somePreference ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
cell.accessoryView = nil;
Swift 5、iOS 17:
tintColor = .white
accessoryView = UIImageView(image: UIImage(systemName: "chevron.right"))
您可以自定义箭头:
let config = UIImage.SymbolConfiguration(pointSize: 17, weight: .bold)
let image = UIImage(systemName: "chevron.right", withConfiguration: config)
accessoryView = UIImageView(image: image)