【问题标题】:How to change color of disclosure indicator of UITableVeiw Programmatically? [duplicate]如何以编程方式更改 UITableVeiw 的披露指示器的颜色? [复制]
【发布时间】:2013-10-21 10:59:02
【问题描述】:

我知道使用UIImageView我们可以设置Disclosure Indicator Accessory,但我想只更改Disclosure Indicator的颜色而不使用UIImageView

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

【问题讨论】:

  • 你到底是什么意思?您始终可以使用自定义单元格
  • 老问题,但还没有满意的答案。这是您实际要求的内容:stackoverflow.com/a/35427702/378024

标签: ios iphone objective-c uitableview


【解决方案1】:

添加您自己的披露指标:

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

【讨论】:

【解决方案2】:

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

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.

【讨论】:

  • 谢谢,这很好用!我只是稍微更改了绘图,因为视图太大而且线条太粗而无法满足我的需要,除此之外,它对我来说是完美的。
  • 你能附上一张图片吗?谢谢。
  • 或者在 Swift 中:pastebin.com/xgReAeWc
  • 效果很好,但我同意@AndraTodorescu 的观点,因为箭头有点太胖,不适合 iOS 7+ 的审美。我将 this example's CGContextSetLineWidth(ctxt, 2); 修改为 2 而不是 4 以获得所需的结果。
【解决方案3】:

抱歉,我迟到了,但我今天为此苦苦挣扎,只是想通了(在 iOS8 和 9 中)。使用 View Debugger,很明显 Disclosure 三角形是 UIButton 中 UIImageView 中的 UIImage。

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

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

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

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

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

【讨论】:

  • 哦,我应该补充一点,只要您的单元格的视图层次结构中没有其他按钮,它就可以工作。如果你这样做了,你需要给它们一个区别标签或其他东西,这样你就可以在寻找正确的按钮时忽略它们。
  • awakeFromNib 中添加self.tintColor = .red 在通过subViews 之前,我认为会使这个子类更加清晰。
【解决方案4】:

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

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

一个用于自定义披露指标的库:check here

【讨论】:

    【解决方案5】:

    如上所述,修改附件视图的一种方法是添加您自己的 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
    

    【讨论】:

    • 不起作用。图标显示为 ?在盒子里。这个想法很好
    • @JoãoNunes 您是否将 icomoon 添加到项目中?
    • @MarkBlythe 不。如果我们需要,那么答案中不会提及。
    • @JoãoNunes Icomoon 是一种自定义字体,您需要将其添加到应用程序中,否则您将得到“?”应用程序尝试呈现它无法识别的字体。这在未来可能会有所帮助:codewithchris.com/…
    • @njzk2 显然不是全部
    【解决方案6】:

    您可以尝试使用 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];
    

    【讨论】:

      猜你喜欢
      • 2012-07-09
      • 1970-01-01
      • 2018-01-31
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 2012-07-07
      • 2016-07-03
      相关资源
      最近更新 更多