UITableViewCell配件没有着色

Tok*_*iku 11 accessory uitableview ios swift

我已经阅读了关于这个主题的多个问答,但似乎都没有,所以这是我的问题.

我创建了一个自定义的UITableViewCell,在故事板中,我要求有一个披露指示器附件.据说tintColor应该改变指示器的颜色,但经过大量研究后,我发现了以下内容:

  • 单元格的accessoryView映射为零值,因此我不能影响它的tintColor

我尝试使用selectedBackgroundView创建accessoryView,如下所示:

self.accessoryView = UIView()
Run Code Online (Sandbox Code Playgroud)

显然,它只是创建一个空白区域,原始的公开配件消失了.我真的很困惑这一切,无法找到影响细胞配件颜色的方法.任何帮助都会受到欢迎!

Zel*_*lko 27

延期

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

斯威夫特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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

做吧

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.prepareDisclosureIndicator()
}
Run Code Online (Sandbox Code Playgroud)

迅捷3:

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

}
Run Code Online (Sandbox Code Playgroud)

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];
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.很好,干净,容易.任何想知道如何设置默认蓝色以外的颜色的人.只需将tintColor设置为"button","button.tintColor = .redColor()" (2认同)

Tok*_*iku 15

这有助于我,应该帮助别人.

任何UIView类型和子类型的tintColor属性都会将其色调设置传播到其层次结构中的子视图.您可以为UITableView设置tintColor,它将应用于其中的所有单元格.

尽管如此,并不是所有UITableViewCell附件类型都可能被染色.

有染色的人:

  • UITableViewCellAccessoryCheckmark
  • UITableViewCellAccessoryDe​​tailButton
  • UITableViewCellAccessoryDe​​tailDisclosureButton - >只有Details部分

以下不会被染色:

  • UITableViewCellAccessoryDisclosureIndicator
  • UITableViewCellAccessoryDe​​tailDisclosureButton - >只有Disclosure Button

因此,通常,您将能够更改UITableViewCell附件的颜色.但是如果你想将通常表示segue的灰色箭头更改为另一个视图,则没有机会,它将保持灰色.

更改它的唯一方法是实际创建自定义UIAccessoryView.这是一个有目的地细分的实现,以保持清晰.虽然我相信存在更好的方法:

在我的awakeFromNib()方法中的自定义UITableViewCell类中

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

请注意,这也不能着色.与"Tab Bar Items"相比,它将使用所用图像的颜色,因此您可能需要多个图像用于选定的单元格和未选择的单元格.

  • 您可以使用`imageWithRendingMode`和`UIImageRenderingModeAlwaysTemplate`轻松地为您的显示图像着色.然后在`UIImageView`(或superview)上使用`tintColor` (8认同)