34

我知道使用UIImageView我们可以设置披露指示器附件,但我只想更改披露指示器颜色而不使用UIImageView

有可能吗?如果有可能那怎么办?

4

6 回答 6

43

添加您自己的披露指标:

cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessory.png"]];
于 2013-10-21T11:03:06.963 回答
4

我知道我迟到了,但我刚刚准备了一个自定义视图,该视图绘制了一个披露视图。我很快就做了一个演示,所以它可能不是非常精确,它只是完成了工作。希望它可以帮助某人:

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

然后在我的自定义单元格中,我这样做:

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

从理论上讲,您也应该能够在初始化后更改其颜色,还没有尝试过。

看起来像这样: 视图的示例用法

Z。

于 2014-10-10T17:06:39.237 回答
3

抱歉,我参加聚会超级迟到了,但我今天一直在努力解决这个问题,只是想通了(在 iOS8 和 9 中)。使用 View Debugger,很明显 Disclosure 三角形是 UIButton 中的 UIImageView 中的 UIImage。

所以在 -awakeFromNib 中,我正在遍历子视图以找到一个按钮。找到按钮后,可以通过 -backgroundImageForState 获取对原始图像的引用:

获得原始图像后,您可以通过调用 [originalImage imageWithRenderingMode:AlwaysTemplate] 创建模板图像的副本

然后,您可以为所有可用状态设置 backgroundImage。完成此操作后,图像会自动拾取色调颜色。

下面是一些快速的示例代码:

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
            }
        }  
    }
}
于 2015-10-20T20:18:38.003 回答
1

您必须创建自己的自定义 UIButton 并将其设置为单元格的附件视图。你不能通过简单地指定它的 UIColor 来改变它的颜色。

您可以通过为正常状态添加一个图像和为选定(突出显示)添加另一个图像来自定义披露指示器。

一个用于自定义披露指标的库:在此处查看

于 2013-10-21T11:05:52.040 回答
1

如上所述,修改附件视图的一种方法是添加您自己的 UIImageView。然而,事实上你可以提供任何从 UIView 派生的对象。然后我会推荐使用带有图标字体(例如 icomoon)的 UILabel,而不是 UIImageView。UILabel 和图标字体允许图像、颜色和大小的灵活性。

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
于 2015-03-03T16:02:54.203 回答
0

您可以尝试使用 uiimageview 来应用单元配件类型,如下面的代码:

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];
于 2013-10-21T11:13:58.247 回答