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 able to figure out which one's the optimum. So here is what I think I can do.
There is a property of UITableViewCell
- accessoryView
. So I can use setAccessoryView:(UIView *)view
and pass view as the UIImageView
holding the image that I want.
I have written an utility class which creates the content view (stuff like background color, adding other stuff, etc) for my cell and I add this content view to the cell in UITableViewDelegate
. The other option is to draw a UIImage
overriding the drawRect
method of CustomContentView
utility class.
Performing option 1 - I can get the things done the apple way. Just give them the view and they do the rest. But I guess adding a new UIView
object to every row might turn out to be a heavy object allocation and decreasing the frame rate. As compared to just a UIImage
object in my contentView
. I believe UIImage
is lighter than UIView
.
Please throw some light people and help me decide over it.
You cant change its color by by simply specifying its UIColor. You can customize Disclosure Indicator by adding one image for normal state and another for selected (highlighted).
If you want the '>' show up by default, goto the storyboard, click on the cell, goto the fourth tab on the right hand side, select the accessory as 'Disclosure Indicator'. ATTENTION! The real option you want is Disclosure Indicator , not None .
Great post on Cocoanetics that addresses this. The UIControl class inherits the properties selected, enabled and highlighted Custom-Colored Disclosure Indicators
If you're interested in drawing the indicator, instead of using an image file, here's code I worked out to do so:
// (x,y) is the tip of the arrow CGFloat x = CGRectGetMaxX(self.bounds) - RIGHT_MARGIN; CGFloat y = CGRectGetMidY(self.bounds); const CGFloat R = 4.5; CGContextRef ctxt = UIGraphicsGetCurrentContext(); CGContextMoveToPoint(ctxt, x-R, y-R); CGContextAddLineToPoint(ctxt, x, y); CGContextAddLineToPoint(ctxt, x-R, y+R); CGContextSetLineCap(ctxt, kCGLineCapSquare); CGContextSetLineJoin(ctxt, kCGLineJoinMiter); CGContextSetLineWidth(ctxt, 3); // If the cell is highlighted (blue background) draw in white; otherwise gray if (CONTROL_IS_HIGHLIGHTED) { CGContextSetRGBStrokeColor(ctxt, 1, 1, 1, 1); } else { CGContextSetRGBStrokeColor(ctxt, 0.5, 0.5, 0.5, 1); } CGContextStrokePath(ctxt);
If you make a custom UIView subclass, do the above in the drawRect: method, and use that as your accessory view, you'll be able to make the color anything you want.
An accessory view (custom or UIImageView won't be a major performance problem as long as you are properly recycling UITableViewCell instances.
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
@interface UITableViewCell (DisclosureIndicatorColor)
@property (nonatomic, strong) UIColor *disclosureIndicatorColor;
- (void)updateDisclosureIndicatorColorToTintColor;
@end
@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
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.
Use an UIImageView. This will also allow you to change the image when the cell is selected:
UIImageView* arrowView = [[UIImageView alloc] initWithImage:normalImage];
arrowView.highlightedImage = selectedImage;
cell.accessoryView = arrowView;
[arrowView release];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With