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

前端 未结 12 1906
有刺的猬
有刺的猬 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:29

    Change the tint colour of table view cell. Check the screenshot to do the same via Storyboard.

    0 讨论(0)
  • 2020-12-04 10:30

    Great post on Cocoanetics that addresses this. The UIControl class inherits the properties selected, enabled and highlighted Custom-Colored Disclosure Indicators

    0 讨论(0)
  • 2020-12-04 10:32

    In swift 3, I have adapted the solution from @galambalazs as a class extention as follows:

    import UIKit
    
    extension UITableViewCell {
    
        func setDisclosure(toColour: UIColor) -> () {
            for view in self.subviews {
                if let disclosure = view as? UIButton {
                    if let image = disclosure.backgroundImage(for: .normal) {
                        let colouredImage = image.withRenderingMode(.alwaysTemplate);
                        disclosure.setImage(colouredImage, for: .normal)
                        disclosure.tintColor = toColour
                    }
                }
            }
        }
    }
    

    Hope this helps some.

    0 讨论(0)
  • 2020-12-04 10:32

    benzado's solution works fine, but it showed a black background. In the UIView class that you setup (the one who's drawRect function you put in his code) needs to have the following initWithFrame implementation in order for the disclosure drawing to have a transparent background:

    - (id)initWithFrame:(CGRect)frame {
    
        self = [super initWithFrame:frame];
        if (self) {
            [self setBackgroundColor:[UIColor clearColor]];
            // Initialization code.
        }
        return self;
    }
    

    Naturally, you can set this to whatever color you want...

    0 讨论(0)
  • 2020-12-04 10:33

    But I guess adding a new UIView object to every row might turn out to be a heavy obj allocation and decreasing the frame rate. As compared to just a UIImage object in my contentView. I believe UIImage is lighter than UIView.

    Drawing an image directly will almost certainly have better performance than adding a subview. You have to determine if that extra performance is necessary. I've used a few accessory views for custom disclosure indicators on basic cells and performance was fine. However, if you're already doing custom drawing for the content rect, it might not be that difficult to do the accessory view also.

    0 讨论(0)
  • 2020-12-04 10:34

    Here is an implementation that works in iOS 8+. It does exactly what's asked for:
    change the color of the original Apple disclosure indicator to a custom color.
    Use it like this:

    #import "UITableViewCell+DisclosureIndicatorColor.h"
    // cell is a UITableViewCell
    cell.disclosureIndicatorColor = [UIColor redColor]; // custom color
    [cell updateDisclosureIndicatorColorToTintColor]; // or use global tint color
    

    UITableViewCell+DisclosureIndicatorColor.h

    @interface UITableViewCell (DisclosureIndicatorColor)
    @property (nonatomic, strong) UIColor *disclosureIndicatorColor;
    - (void)updateDisclosureIndicatorColorToTintColor;
    @end
    

    UITableViewCell+DisclosureIndicatorColor.m

    @implementation UITableViewCell (DisclosureIndicatorColor)
    
    - (void)updateDisclosureIndicatorColorToTintColor {
        [self setDisclosureIndicatorColor:self.window.tintColor];
    }
    
    - (void)setDisclosureIndicatorColor:(UIColor *)color {
        NSAssert(self.accessoryType == UITableViewCellAccessoryDisclosureIndicator,
            @"accessory type needs to be UITableViewCellAccessoryDisclosureIndicator");
    
        UIButton *arrowButton = [self arrowButton];
        UIImage *image = [arrowButton backgroundImageForState:UIControlStateNormal];
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        arrowButton.tintColor = color;
        [arrowButton setBackgroundImage:image forState:UIControlStateNormal];
    }
    
    - (UIColor *)disclosureIndicatorColor {
        NSAssert(self.accessoryType == UITableViewCellAccessoryDisclosureIndicator, 
            @"accessory type needs to be UITableViewCellAccessoryDisclosureIndicator");
    
        UIButton *arrowButton = [self arrowButton];
        return arrowButton.tintColor;
    }
    
    - (UIButton *)arrowButton {
        for (UIView *view in self.subviews)
            if ([view isKindOfClass:[UIButton class]])
                return (UIButton *)view;
        return nil;
    }
    
    @end
    
    0 讨论(0)
提交回复
热议问题