34

I know that using UIImageView We can set the Disclosure Indicator Accessory, But I want to change only disclosure indicator color without using UIImageView.

Is it Possible or Not? If it is possible then how?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bhavesh
  • 1,422
  • 1
  • 12
  • 31
  • How do you mean exactly? you can always use custom cells – Raon Oct 21 '13 at 11:02
  • Old question but had no satisfactory answer yet. Here is what you actually asked for: http://stackoverflow.com/a/35427702/378024 – gblazex Feb 16 '16 at 08:51

6 Answers6

43

Add your own disclosure indicator:

cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessory.png"]];
RFG
  • 2,880
  • 3
  • 28
  • 44
  • 1
    Thanks for reply, I know about this but I want to change the color without using UIImageView like we can change the tint color of slider. Is it possible? – Bhavesh Oct 21 '13 at 11:08
  • 2
    As far I know (not to much) it's not possible. Another possibility is, instead using a png, draw the indicator. Try with this link: http://www.cocoanetics.com/2010/10/custom-colored-disclosure-indicators/ – RFG Oct 21 '13 at 11:42
  • **It is possible.** See my answer how to **color the original Apple indicator:** http://stackoverflow.com/a/35427702/378024 (without using custom images or custom drawing code) – gblazex Feb 16 '16 at 08:50
  • http://www.cocoanetics.com/2010/10/custom-colored-disclosure-indicators/ its worked for me.... Thank you so much RFG – Hari Narayanan Mar 29 '16 at 15:11
  • 2
    @galambalazs Your answer works great, but please correct the link to be http://stackoverflow.com/a/35427599/496504 – Jamie McDaniel Jun 08 '16 at 12:20
4

I know I'm late to the party but I just prepared a custom view that draws a disclosure view. I've quickly made it for a demo so it might not be extremely precise, it just does the job. Hope it helps someone:

PZDisclosureIndicator.h

//
// Created by Pall Zoltan on 10/10/14.
// Copyright (c) 2014 pallzoltan. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface PZDisclosureIndicator : UIView
@property(nonatomic, strong) UIColor *color;

- (PZDisclosureIndicator *)initWithColor:(UIColor *)color;
@end

PZDisclosureIndicator.m:

//
// Created by Pall Zoltan on 10/10/14.
// Copyright (c) 2014 pallzoltan. All rights reserved.
//

#import "PZDisclosureIndicator.h"

@interface PZDisclosureIndicator ()

@property(nonatomic) CGFloat red;
@property(nonatomic) CGFloat green;
@property(nonatomic) CGFloat blue;
@property(nonatomic) CGFloat alpha;

@end

@implementation PZDisclosureIndicator

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, self.red, self.green, self.blue, self.alpha);

    CGContextMoveToPoint(context, 4, 0);
    CGContextAddLineToPoint(context, 4, 0);
    CGContextAddLineToPoint(context, 16, 12);
    CGContextAddLineToPoint(context, 4, 24);
    CGContextAddLineToPoint(context, 0, 24 - 4);
    CGContextAddLineToPoint(context, 9, 12);
    CGContextAddLineToPoint(context, 0, 4);
    CGContextAddLineToPoint(context, 4, 0);
    CGContextFillPath(context);
}

- (PZDisclosureIndicator *)initWithColor:(UIColor *)color {
    self = [super initWithFrame:CGRectMake(0, 0, 16, 24)];
    self.backgroundColor = [UIColor clearColor];

    self.color = color;
    [self setNeedsDisplay];

    return self;
}

- (void)setColor:(UIColor *)color {
    _color = color;

    [self.color getRed:&_red green:&_green blue:&_blue alpha:&_alpha];

    [self setNeedsDisplay];
}

@end

Then in my custom cell, I do this:

self.accessoryView = [[PZDisclosureIndicator alloc] initWithColor:SECONDARY_HIGHLIGHT_COLOR];

Theoretically you should be able to change its colour after initialisation too, haven't tried that.

Looks like this: Sample usage of the view

Z.

Ryan Brodie
  • 6,554
  • 8
  • 40
  • 57
Zoltán
  • 1,422
  • 17
  • 22
  • Thanks, this works great! I've changed the drawing just a bit because the view was too big and the line was too thick for what I needed, other than that, it was perfect for me. – Andra Todorescu Oct 17 '14 at 14:47
  • Can you include a picture? Thanks. – testing Dec 05 '14 at 13:33
  • Or in Swift: http://pastebin.com/xgReAeWc – vrwim Apr 20 '15 at 14:31
  • Works well but I agree with @AndraTodorescu in that the arrow is a bit too fat to suit the aesthetic of iOS 7+. I modified [this example's](https://www.cocoanetics.com/2010/10/custom-colored-disclosure-indicators/) `CGContextSetLineWidth(ctxt, 2);` to be 2 and not 4 to get the desired result. – Ryan Brodie Nov 03 '15 at 12:00
3

Sorry, I'm super super late to the party but I was struggling with this today and just figured it out (in iOS8 and 9). Using the View Debugger, it was clear the Disclosure triangle is a UIImage in a UIImageView in a UIButton.

So in -awakeFromNib, I was iterating through the subviews to find a button. Once I found the button, a reference to the original image could be acquired via -backgroundImageForState:

Once you get the original image, you can create a copy that is a Template image by calling [originalImage imageWithRenderingMode:AlwaysTemplate]

Then you can set the backgroundImage for all available states. Once you do that, the Image picks up the tint color automatically.

Below is some swift sample code:

class GratuitousDisclosureTableViewCell: UITableViewCell {

    private weak var disclosureButton: UIButton? {
        didSet {
            if let originalImage = self.disclosureButton?.backgroundImageForState(.Normal) {
                let templateImage = originalImage.imageWithRenderingMode(.AlwaysTemplate)
                self.disclosureButton?.setBackgroundImage(templateImage, forState: .Normal)
                self.disclosureButton?.setBackgroundImage(templateImage, forState: .Highlighted)
                self.disclosureButton?.setBackgroundImage(templateImage, forState: .Disabled)
                self.disclosureButton?.setBackgroundImage(templateImage, forState: .Selected)
                self.disclosureButton?.setBackgroundImage(templateImage, forState: .Application)
                self.disclosureButton?.setBackgroundImage(templateImage, forState: .Reserved)
            }
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        for view in self.subviews {
            if let button = view as? UIButton {
                self.disclosureButton = button
                break
            }
        }  
    }
}
Jeffrey Bergier
  • 229
  • 1
  • 8
  • 1
    Oh, I should add, that this works as long as you don't have other buttons in your cell's view hierarchy. If you do, you will need to give them a distinguishing tag or something so you can ignore them while looking for the correct button. – Jeffrey Bergier Oct 20 '15 at 20:19
  • Adding `self.tintColor = .red` in `awakeFromNib` before stepping through the `subViews` would make this subclass a little clearer I think. – Daniel Storm Aug 26 '17 at 01:13
1

You have to create your own custom UIButton and set it as cell's accessoryView. 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).

one Library for customizing Disclosure Indicator: check here.

Mumthezir VP
  • 6,251
  • 6
  • 28
  • 57
1

As pointed above one way to modify an accessoryView is by adding your own UIImageView. However, in fact you can supply any object deriving from UIView. I would then recommend using a UILabel with an icon font (e.g. icomoon) instead of UIImageView. UILabel and an icon font allow for flexibility in both image, color and size.

let font = UIFont(name: "icomoon", size: 16.0)
let icon = "\u{e60f}"    
let lbl = UILabel(frame: CGRectMake(0, 0, 20, 20))
lbl.font = font
lbl.text = icon
cell.accessoryView = lbl
Gooshan
  • 2,361
  • 1
  • 20
  • 15
  • 1
    doesn't work. The icon shows as ? in a box. The idea is good though – João Nunes May 30 '15 at 10:05
  • @JoãoNunes did you add icomoon to the project? – MÖRK Aug 06 '15 at 19:43
  • @MarkBlythe nope. If we need that, then it isn't mention in the answer. – João Nunes Aug 07 '15 at 12:44
  • @JoãoNunes Icomoon is a custom font, you will need to add it to the app or you will end up with '?' where the app tries to render a font it doesn't recognise. This may help a little in the future: http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/ – MÖRK Aug 07 '15 at 12:52
  • @MarkBlythe Im just saying that the answer doesn't mention that you need to add a costume font. It must be edited. – João Nunes Aug 07 '15 at 13:33
  • @JoãoNunes it is written here: `UIFont(name: "icomoon"`. Several people at least would recognize that not to be a font embedded by default in iOS. – njzk2 Sep 08 '15 at 18:33
  • 3
    @njzk2 not all apparently – João Nunes Sep 09 '15 at 19:31
0

You can try with uiimageview to apply cell accessoryType like below code:

cell.accessoryView = myAccessoryUIImageView;
cell accessoryview uiimageview not align correctly try below code:


UIView* accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 24, 50)];
UIImageView* accessoryViewImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessory_image.png"]];
accessoryViewImage.center = CGPointMake(12, 25);
[accessoryView addSubview:accessoryViewImage];
[cell setAccessoryView:accessoryView];
TechFanatic
  • 1,218
  • 4
  • 13
  • 31