Change color on checkmark in UITableView - objective-c

Could someone 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.

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
}

The above answers are all great. But if you want to do it globally, just do
UITableViewCell.appearance().tintColor = .green
Nice little trick :)

Related

Objective C > UITableViewCell button not depressing

I have the following button being created in the cellForRowAtIndexPath section for this tableView:
UIButton *sched_button_a1 = [UIButton buttonWithType:UIButtonTypeCustom];
sched_button_a1.frame = CGRectMake(0, 0, 110, 52);
CGRect frame_a1 = sched_button_a1.frame;
// Resize height based on length of appointment
frame_a1.size.height = heightCalc;
sched_button_a1.frame = frame_a1;
[sched_button_a1 addTarget:self action:#selector(showAppointment:) forControlEvents:UIControlEventTouchUpInside];
[sched_button_a1 setTitle:[NSString stringWithFormat:#"APPT: %#", APPT_ID ] forState:UIControlStateNormal];
sched_button_a1.layer.backgroundColor = [UIColor redColor].CGColor;
[sched_a1 addSubview:sched_button_a1];
As you can see it's calling showAppointment which is presently just:
-(void)showAppointment:(UIButton*)sender {
NSLog(#"Button pushed");
}
showAppointment is also defined here:
-(void)showAppointment:(UIButton*)sender;
The button shows up fine and gets the correct text and background and appears to be in the foreground, however, when I click on it nothing happens and it doesn't look like the button even gets depressed.
What am I doing wrong or missing?
Edit: I have gone back and removed the sched_a1 view so that the button is directly created in the contentView and this had no effect. It still appears that the only gesture being recognized is the one used to scroll the table. Tapping the button does not appear to change the color of the text or otherwise indicate that the button has been pressed and no log entry is created.
Edit 2: Added cellForRowAtIndexPath to paste bin
http://pastebin.com/WXezfW96
I had a similar issue recently, which I struggled to understand. The solution I came up with was to set the bounds of the button as well and also check for UIControlEventTouchDown rather than UIControlEventTouchUpInside.
Have you tried using cancelsTouchesInView? If you added tap gesture, I think the tap gesture or the tap in table cell overrides the touches in your button. Try using this code:
UIGestureRecognizer *tap= [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(yourAction)];
tap.cancelsTouchesInView = NO;
tap.delegate = self;
[yourButton addGestureRecognizer:tap];
But don't forget to add <UIGestureRecognizerDelegate> in your .h file
But I agree on Mike. Make it short and simple when creating table cell. I think you can use custom cell subclass and xib file.
I'm also new to objective C so if I say something wrong I'm sorry, I'm just trying to help.
UIButton *sched_button_a1 = [UIButton buttonWithType:UIButtonTypeCustom];
in this change UIButtonTypeCustom to UIButtonTypeRoundedRect
Scott, you may want to try my famous troubleshooting technique:
Determine whether the button or the action is the root cause:
UIImage *image = [UIImage imageNamed:#"image.png"];
[button setBackgroundImage:image forState:UIControlStateHighlighted|UIControlStateSelected]
Instead of:
-(void)showAppointment:(UIButton*)sender {
try
-(void)showAppointment:(id*)sender {
Use breakpoints.
If none of the above works consider refactoring your code that looks a bit messy.
Hey i have checked the code you wrote, it is working fine. Can you check if you have written similar to the code below in your cellForRowAtIndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Identifier = #"Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
}
UIButton *sched_button_a1 = [UIButton buttonWithType:UIButtonTypeCustom];
sched_button_a1.frame = CGRectMake(0, 0, 110, 52);
CGRect frame_a1 = sched_button_a1.frame;
[sched_button_a1 addTarget:self action:#selector(showAppointment:) forControlEvents:UIControlEventTouchUpInside];
[sched_button_a1 setTitle:#"Your Text" forState:UIControlStateNormal];
sched_button_a1.layer.backgroundColor = [UIColor redColor].CGColor;
[cell.contentView addSubview:sched_button_a1];
return cell;
}
I was not able to debug further as you have not kept your entire cellForRowAtIndexPath code. Hope this could help you to solve your problem
Since you are using a button type of UIButtonTypeCustom, you will need to set the attributes for UIControlStateHighlighted and UIControlStateSelected so that you achieve the effect you want for appearing to press the button.
You can change the title color or the background image for the highlighted and selected states using one of the following UIButton methods:
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
As an additional note regarding the code in the cellForRowAtIndexPath, you shouldn't be getting the data from sql in the method it would be destructive to your table view performance, get all the data you need before and have them stored in object in an array and then in the cellForRowAtIndexPath method you would just update the values labels from the object for the row.

change UISearchBar text field background image for iOS-7

I am specifically looking for an iOS-7 answer. If I had an image, the answer would be
[mySearchBar setSearchFieldBackgroundImage:myImage forState:UIControlStateNormal];
But I just want to apply a color. Is there a simple way of doing this with just a color? Again, for iOS-7.
Note: I see a host of very complicated answers here on SO: with traversing, etc. I hoping for something simple and that won't cause my app to be rejected.
If I understand correctly, you are looking for the barTintColor property. You can set the barTintColor property as such,
mySearchBar.barTintColor = [UIColor blackColor];
or
[mySearchBar setBarTintColor:[UIColor blackColor];
Hope that helps!
First you need to find the UITextField inside the search bar and then change its color. Here is the code to get the text field:
for (UIView *subView in searchBar.subviews)
{
for (UIView *secondLevelSubview in subView.subviews){
if ([secondLevelSubview isKindOfClass:[UITextField class]])
{
UITextField *searchBarTextField = (UITextField *)secondLevelSubview;
return searchBarTextField;
}
}
}
return nil;

UITableViewCell custom selectedBackgroundView background is transparent

I have the following code that creates a UIView that I assign to my UITableViewCell's selectedBackgroundView property. Everything works as expected, with the exception of the subview's background, which is transparent.
I use the same code to create a custom view that I assign to backgroundView, and that works fine.
What is causing that subview to be transparent for selectedBackgroundView, and how can I avoid that?
- (UIView*) makeSelectedBackgroundView
{
// dimensions only for relative layout
CGRect containerFrame = CGRectMake(0, 0, 320, 40);
UIView* containerView = [[UIView alloc] initWithFrame:containerFrame];
containerView.autoresizesSubviews = YES;
// dimensions only for relative layout
CGRect subframe = CGRectMake(5, 5, 310, 30);
UIView* subview = [[UIView alloc] initWithFrame:subframe];
subview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
subview.backgroundColor = [UIColor redColor];
subview.layer.cornerRadius = 5;
subview.layer.borderWidth = 2;
subview.layer.borderColor = [UIColor greenColor].CGColor;
[containerView addSubview:subview];
return containerView;
}
As we can see from name of ivar selectedBackgroundView, this background shown by cell when it was selected.
I've to reload few methods (– setSelected:animated: and – setHighlighted:animated:) of UITableViewCell subclass to reset background color of subviews back to their values. Look's like UIKit do some magic in this template methods (iterating over all UIView subclasses and set their background to clearColor)
This code might be helpful for you:
UIImageView *cellImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(0,
0,
cell.frame.size.width,
cell.frame.size.height
)];
cellImageView.contentMode = UIViewContentModeScaleAspectFit;
// normal background view
[cellImageView setImage:[UIImage imageNamed:#"*<ImageName>*"]];
[cell addSubview:cellImageView];
[cell sendSubviewToBack:cellImageView];
[cellImageView release], cellImageView = nil;
Here cell is an object of custom UITableViewCell.
Also you can set backgroundColor property.
I would try to set the alpha for both containerView and subView to 1.0
[containerView setAlpha:1.0];
...
[subview setAlpha:1.0];
this should make your controls totally opaque.
You could also create some images for the background and use that images in state of creating 2 views. Let's say you create 2 image (normalBackground.png and selectedBackground.png) and then set this images as cell background. Here is a nice tutorial.
Try setOpaque:YES on your views.
In the end, I ended up subclassing UITableViewCell which contained a custom view object, and that worked.

Objective C: How to make background color of UITableView Consistent

I have been trying to set the background color of my table view but am facing an issue
This is what I am trying to do.
//Set background color of table view (translucent)
self.tableView.backgroundColor = [UIColor colorWithRed:0.0 green:0.2 blue:0.5 alpha:0.7];
//Set frame for tableview
[self.tableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.picker.frame.size.height)];
After setting the table cells, I saw that there is an inconsistency in the alpha level around the table view cell (see screenshot below)
Is there anyway to make the color / alpha level consistent for the background?
(Note: I am not setting a background image, only color and alpha level)
Thanks!
Zhen Hoe
I recently ran into this problem myself and think I found the solution
self.tableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundColor = [UIColor colorWithRed:0.0 green:0.2 blue:0.5 alpha:0.7];
Give that a shot, might need some massaging for your specific case (looks like you have some picture behind that translucent blue too).
You need to make the background of the UITableView clear as well as set Opaque to FALSE
[self.tableView setBackgroundColor: [UIColor clearColor]];
[self.tableView setOpaque: NO];
All you need to do are 2 things:
E.g. A view has a table view in it.
In the XIB file set the background of the UITableView to clear color.
Set the background of the container view of the table view to the image you want.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"app_background.png"]];
If the ViewController is actually a TableViewController then set:
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"app_background.png"]];
You can set the row background with inside the table view delegate (the view or tableview controller)
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] % 2 == 0) {//248 G:192 B:100
[cell setBackgroundColor: [UIColor redColor]];
} else {
[cell setBackgroundColor: [UIColor colorWithPatternImage:[UIImage imageNamed:#"xyz.png"]]];
}
}

Changing UITableViewCell textLabel background color to clear

In my app I have a table view with customViewCells. I subclassed the UITableViewCell class and added an image that will load async and for the text I use cell.textLabel.text = #"someThext".
For the cells the background color is set alternatively to [UIColor darkGrayColor] and [UIColor whiteColor].
When I run the app in the simulator and on the phone the textLabel of the cell has the background white. I want to set it to be clear, because I want the background color of the cell to be full not a strip then white then another strip.
In the init method of my custom cell I added, hoping that the white will turn into red, but it doesn't have any effect:
[self.textLabel setBackgroundColor:[UIColor redColor]];
I tried also:
self.textLabel.backgroundColor = [UIColor redColor];
But this also didn't work... if I add a UILabel as a subview, the background color of the label can be set, but I don't want to do that because when I rotate the phone I want my labels to auto enlarge.
Any ideas why setting the background color of cell.textLabel doesn't work?
Thank you
If you do not want to subclass UITableViewCell you can just add this:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
[[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];
}
The problem is that UIKit sets the cell background color in the -setSelected method. I had the method but didn't have self.textLabel.backgroundColor = [UIColor clearColor]; self.detailTextLabel.backgroundColor = [UIColor clearColor]; in it so I added them and the problem mentioned in the picture was fixed.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
self.textLabel.backgroundColor = [UIColor clearColor];
self.detailTextLabel.backgroundColor = [UIColor clearColor];
}
Looks like Apple changed something here. Doing exactly this in iOS4 works:
self.textLabel.backgroundColor = [UIColor xxxColor];
self.detailTextLabel.backgroundColor = [UIColor xxxColor];
At least up to the point that the label background is transparent or takes the background color. Still not possible to set an own background color.
Nice that this is fixed, but a bit surprising during tests if you develop with base SDK 4.1 and min. deployment 3.1 for iPhone classic and iPad.
To set a clear background color you can use clearColor:
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
Is this what you mean?
see this post: How do I set UITableViewCellSelectionStyle property to some custom color?
in your case just use a UIView with white background color
I didn't try it but here's a guess... In order to draw faster the opaque property of the label might be set to true by default.
Try
cell.titleLabel.opaque = NO;
cell.titleLabel.backgroundColor = [UIColor clearColor];
If that doesn't work either I would probably just give up at this point and create my own UILabel for the cell.
to set the color of the textlabel you should do this:
t.textColor = [UIColor colorYouWantRGB];