Which is the best way to change the color/view of disclosure indicator accessory view in a table view cell in iOS?

前端 未结 12 1950
有刺的猬
有刺的猬 2020-12-04 10:26

I need to change the color of the disclosureIndicatorView accessory in a UITableViewCell. I think there are two ways to get this done, but I\'m not

12条回答
  •  离开以前
    2020-12-04 10:49

    While galambalazs' answer works, it should be noted that it is somewhat of a hack as it indirectly accesses and updates Apple's private implementation of the disclosure indicator. At best, it could stop working in future iOS releases, and at worst lead to App Store rejection. Setting the accessoryView is still the approved method until Apple exposes a property for directly setting the color.

    Regardless, here is the Swift implementation of his answer for those who may want it:

    Note: cell.disclosureIndicatorColor has to be set after cell.accessoryType = .DisclosureIndicator is set so that the disclosureIndicator button is available in the cell's subviews:

    extension UITableViewCell {
    
        public var disclosureIndicatorColor: UIColor? {
            get {
                return arrowButton?.tintColor
            }
            set {
                var image = arrowButton?.backgroundImageForState(.Normal)
                image = image?.imageWithRenderingMode(.AlwaysTemplate)
                arrowButton?.tintColor = newValue
                arrowButton?.setBackgroundImage(image, forState: .Normal)
            }
        }
    
        public func updateDisclosureIndicatorColorToTintColor() {
            self.disclosureIndicatorColor = self.window?.tintColor
        }
    
        private var arrowButton: UIButton? {
            var buttonView: UIButton?
            self.subviews.forEach { (view) in
                if view is UIButton {
                    buttonView = view as? UIButton
                    return
                }
            }
            return buttonView
        }
    }
    

提交回复
热议问题