开发者

Change color on checkmark in UITableView

开发者 https://www.devze.com 2023-04-10 03:13 出处:网络
Could so开发者_高级运维meone be so kind to show me how to change the color on the checkmark in UITableView?

Could so开发者_高级运维meone be so kind to show me how to change the color on the checkmark in UITableView?

I have searched but don't seem to get it to work.

Cheers


Since the iOS SDK has changed since the accepted answer, I thought I'd just update with a new answer.

You can in fact change the color of the checkmark in a UITableViewCell by adjusting the tintColor property of the UITableViewCell.

You can also set an appearance proxy for all UITableViewCells so that ALL instances have a specific tint color unless otherwise specified

[[UITableViewCell appearance] setTintColor:[UIColor redColor]];

Swift: In Swift change the tintcolor to the color you want to change the color of any Accessory Type

cell.tintColor = .black


Apple doesn't provide a public way to change the color of the checkmark so you'll have to do it with an image.

This is very simple, just set the accesoryView property of the cell to a UIImageView containing a checkmark of the correct color.

It'll look like this:

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

Enjoy!


If you are looking for a Swift version:

Directly on the cell

For example in tableView(_:,cellForRowAtIndexPath:)

cell.tintColor = UIColor.redColor()

Using the appearance protocol

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


The following worked for me in iOS 7.

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


This image shows how to do this in storyboards.The Tint color is the checkmark color.

Change color on checkmark in UITableView


I found that igraczech's answer is mostly correct, but with iOS 6 or later, you can just set the tint color of the entire tableview and default items will inherit down.

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

This worked for me and allowed me to color in the checkmark.


Starting iOS 7 you could set the tint color of your view controller's view so that this tint colow will be propageted to all it's child views. So to set your UITableViewCell's checkmark as purple color (for example), in your viewWillAppear method you need to:

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


The UIAccessoryTypeCheckmark (right side) inherits background color of its tableview.

self.tableView.backgroundColor = [UIColor clearColor];


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

It work for me.


 UITableViewCell *cell=(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath];
cell.tintColor=UIColor.whiteColor;
return cell;


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


I always used this easy way:

UITableViewCell *cell = ...;
cell.tintColor = [UIColor greenColor];
cell.accessoryType = UITableViewCellAccessoryCheckmark;


You don't have to use your own image, you can simply change it in your view did load with the following code.

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

Cheers!


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
        }

Change color on checkmark in UITableView


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

UITableViewCell.appearance().tintColor = .green

Nice little trick :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号