ios 如何以编程方式更改 UITableVeiw 的披露指示器的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19492683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change color of disclosure indicator of UITableVeiw Programmatically?
提问by Bhavesh
I know that using UIImageView
We can set the Disclosure Indicator Accessory, But I want to change only disclosure indicator color without using UIImageView
.
我知道使用UIImageView
We 可以设置 Disclosure Indicator Accessory,但我只想更改公开指示器颜色而不使用UIImageView
.
Is it Possible or Not? If it is possible then how?
有可能吗?如果可能,那么如何?
回答by RFG
Add your own disclosure indicator:
添加您自己的披露指标:
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessory.png"]];
回答by Zoltán
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
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:
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.
Z。
回答by Jeffrey Bergier
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.
抱歉,我参加聚会超级迟到了,但我今天为此苦苦挣扎,才想通了(在 iOS8 和 9 中)。使用视图调试器,很明显 Disclosure 三角形是 UIButton 中 UIImageView 中的 UIImage。
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:
所以在 -awakeFromNib 中,我遍历子视图以找到一个按钮。找到按钮后,可以通过 -backgroundImageForState 获取对原始图像的引用:
Once you get the original image, you can create a copy that is a Template image by calling [originalImage imageWithRenderingMode:AlwaysTemplate]
获得原始图像后,您可以通过调用 [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.
然后您可以为所有可用状态设置 backgroundImage。一旦你这样做了,图像会自动拾取色调颜色。
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
}
}
}
}
回答by Gooshan
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.
如上所述,修改附件视图的一种方法是添加您自己的 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
回答by Mumthezir VP
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.
您必须创建自己的自定义 UIButton 并将其设置为单元格的附件视图。你不能通过简单地指定它的 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.
一个用于自定义披露指标的库:在此处查看。
回答by TechFanatic
You can try with uiimageview to apply cell accessoryType like below code:
您可以尝试使用 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];