How to change color of disclosure indicator of UIT

2020-05-19 04:58发布

问题:

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?

回答1:

Add your own disclosure indicator:

cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessory.png"]];


回答2:

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:

Z.



回答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
            }
        }  
    }
}


回答4:

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.



回答5:

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


回答6:

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];