在UITableView中的复选标记上更改颜色

时间:2011-10-03 22:08:00

标签: objective-c uitableview uicolor

有人可以如此友好地向我展示如何更改UITableView中的复选标记上的颜色吗?

我已搜索但似乎没有让它工作。

干杯

14 个答案:

答案 0 :(得分:199)

由于iOS SDK自已接受的答案后发生了变化,我认为我只是用新的答案进行更新。

您实际上可以通过调整UITableViewCell的{​​{1}}属性来更改tintColor中复选标记的颜色

您还可以为所有UITableViewCell设置外观代理,以便所有实例都具有特定的色调颜色,除非另有说明

UITableViewCell.

答案 1 :(得分:42)

Apple没有提供更改复选标记颜色的公开方法,因此您必须使用图片进行更改。

这很简单,只需将单元格的accesoryView属性设置为包含正确颜色复选标记的UIImageView。

它看起来像这样:

UIImageView *checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"coloredCheckmark.png"]];
cell.accessoryView = checkmark;
[checkmark release];

享受!

答案 2 :(得分:31)

如果您正在寻找Swift版本:

直接在牢房

例如在tableView(_:,cellForRowAtIndexPath:)

cell.tintColor = UIColor.redColor()

使用外观协议

UITableViewCell.appearance().tintColor = UIColor.redColor()

答案 3 :(得分:29)

以下在iOS 7中为我工作。

[self.tableView setTintColor:[UIColor someColor]];

答案 4 :(得分:14)

此图显示了如何在故事板中执行此操作。色调颜色是复选标记颜色。

enter image description here

答案 5 :(得分:13)

我发现igraczech的回答大多是正确的,但是对于iOS 6或更高版本,您可以设置整个tableview的色调颜色,默认项目将继承。

[self.tableView setTintColor:[UIColor someColor]];

这对我有用,并允许我在复选标记上着色。

答案 6 :(得分:8)

启动iOS 7,您可以设置视图控制器视图的色调颜色,以便将此色调颜色调整到其所有子视图。因此,要将UITableViewCell的复选标记设置为紫色(例如),在viewWillAppear方法中,您需要:

[self.view setTintColor:[UIColor purpleColor]];

答案 7 :(得分:4)

UITableViewCell *cell=..;
cell.tintColor=[UIColor whiteColor];
return cell;

答案 8 :(得分:3)

UIAccessoryTypeCheckmark(右侧)继承其tableview的背景颜色。

self.tableView.backgroundColor = [UIColor clearColor];

答案 9 :(得分:2)

#import "UIImage+Color.h"

UIImage *image = [[UIImage imageNamed:@"ic_check_black_24dp.png"] changeColor:CLR_BUY];
UIImageView *checkmark = [[UIImageView alloc] initWithImage:image];
cell.accessoryView = checkmark;

答案 10 :(得分:2)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
HNCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HNCustomTableViewCell" forIndexPath:indexPath];
cell.tintColor = [UIColor colorWithRed:0.99 green:0.74 blue:0.10 alpha:1.0];
return cell;
}

它对我有用。

答案 11 :(得分:1)

您不必使用自己的图像,只需在视图中更改它,并使用以下代码加载。

[self.tableView setTintColor:[UIColor colorWithRed:250/255.0 green:223/255.0 blue:6/255.0 alpha:1]]

干杯!

答案 12 :(得分:1)

The above answers are all great. But if you want to do it globally, just do

UITableViewCell.appearance().tintColor = .green

Nice little trick :)

答案 13 :(得分:0)

Swift 3.1,iOS 9 +

if #available(iOS 9.0, *) {
            UIImageView.appearance(whenContainedInInstancesOf: [UITableViewCell.self]).tintColor = UIColor.themeTint //add your color here 
        } else {
            // Fallback on earlier versions
        }

Result

相关问题