I am trying to create a table with an image as a background.
To achieve this, I started out with the background:
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
This resulted in a background image which appears in tablecells as well. That is not something I want, so I tried to set the cell's backgroundColor:
cell.backgroundColor = [UIColor whiteColor];
This had no effect at all !!!. Hmmm strange.
So I tried this:
UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor whiteColor];
cell.backgroundView = backgroundView;
[backgroundView release];
This works almost. The only problem left is that the textLabel & the detailTextLabel still show the background behind them.
Setting the backgroundColor on those labels to white doesn't do anything either.
How do I proceed? Where do I go wrong? I am trying to achieve a tableView like the worldClock app has.
To change the background colour of the table view cell, you'll need to set it in tableView:willDisplayCell:forRowAtIndexPath: rather than tableView:cellForRowAtIndexPath:
otherwise it won't have any effect, for example:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor whiteColor];
}
Related
I am not able to change the text colors of a UITableViewCell using UIAppearance mechanism.
Here is my code with comments showing what is working for me and what is not:
UITableViewCell *cell = [UITableViewCell appearance];
cell.backgroundColor = [UIColor blueColor]; // working
cell.textLabel.textColor = [UIColor whiteColor]; // NOT WORKING
cell.detailTextLabel.textColor = [UIColor redColor]; // NOT WORKING
UILabel *cellLabel = [UILabel appearanceWhenContainedIn:[UITableViewCell class], nil];
cellLabel.textColor = [UIColor whiteColor]; // working
As you can see, the second way is working, but I cannot set different colors to normal text and detail text.
Is there something that I am doing wrong?
P.S. Defining stuff statically in Interface Builder will not work for me - I have themes which can be changed dynamically during run time.
You're doing the right thing, but there's no way for iOS to distinguish between those two labels using UIAppearance. You should either set the text colour in willDisplayCell, or, if you really want to use UIAppearance, create custom UILabel subclasses that you can target more accurately.
If you want to use willDisplayCell, it would look something like this:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.textLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.textColor = [UIColor redColor];
}
Alternatively, you might also find this answer has some other ideas.
you can set the the text colour like this.
[cell.textLabel setTextColor:[UIColor whiteColor]];
[cell.detailTextLabel setTextColor:[UIColor redColor]];
How do I set the cell selection color for the view controller listed in the More of a tab view controller on iOS 6?
The default is blue, which looks terrible with an app that uses a non-default color. I'd like to set it to a custom color, if possible, but setting it to grey would do.
This problem is specific to iOS 6 because on iOS 7 a grey selection color is used instead.
We can update cell selection color for the view controller listed in the More of a tab view controller on by creating custom datasource for MoreNavigationController.
I have created an sample project which might help - https://github.com/deepthit/CustomizeMoreNavigationController.git
In the custom data source object we can override cellForRowAtIndexPath method to set selectedbackgroundView for a cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [originalDataSource tableView:tableView cellForRowAtIndexPath:indexPath];
// Set background color
UIView *background = [[UIView alloc] initWithFrame:cell.frame];
background.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
background.backgroundColor = [UIColor whiteColor];
cell.backgroundView = background;
// Set selected background color
UIView *selectionColor = [[UIView alloc] initWithFrame:cell.frame];
selectionColor.backgroundColor = [UIColor greenColor];
selectionColor.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
cell.selectedBackgroundView = selectionColor;
return cell;
}
I have a view with a table view, programmatically created (no XIB associated). I want some of the cells to have a light gray background color.
So I tried this :
cell.contentView.backgroundColor = [UIColor colorWithRed:(230/255.f) green:(230/255.f) blue:(230/255.f) alpha:1.0];
But here is the result (the problem is of course that the background of the cell should be entirely gray):
I also tried this, with the same result as previous:
UIView *cell_view = [[UIView alloc] init];
[cell_view setBackgroundColor:[UIColor colorWithRed:(230/255.f) green:(230/255.f) blue:(230/255.f) alpha:1.0]];
[cell setBackgroundView:cell_view];
Nothing seems to work. Thanks for your help.
Set tableviews background color to clear color.
tableView.backgroundColor = [UIColor clearColor];
Do following:
Set tableview background color to clear color.
set cell.backgroundview to nil.
set cell.backgroundcolor to gray color.
Dont give background color. Instead create a view with required color. and set to backgroundView.
Override the tableView:willDisplayCell:forRowAtIndexPath: delegate:
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor:[UIColor colorWithRed:(230/255.f) green:(230/255.f) blue:(230/255.f) alpha:1.0]];
}
Also, set the textLabel background color to transparent
cell.textLabel.backgroundColor = [UIColor clearColor];
try this:
//set this in viewDidLoad method
yourTable.backgroundColor = [UIColor clearColor];
//set this in cellForRowAtIndexPath method of UITableView
cell.backgroundview = nil;
cell.backgroundColor = [UIColor greenColor];
This code is worked for me.
please try this code:
(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
UIColor *CellColor = [UIColor colorWithRed:(230/255.f) green:(230/255.f) blue:(230/255.f) alpha:1.0];
cell.backgroundColor = CellColor;
}
I am trying to make a table cell completely transparent. I am using static table cell in iOS 5 and tried change to clear color, but it does
I even tried (in viewDidLoad)
self.tableView.backgroundColor = [UIColor clearColor];
But the whole table is black and the cell is white. (See photo)
Try this
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor clearColor];
}
self.tableView.backgroundColor = [UIColor clearColor] - will change the background color of the tableview not the cells.
Try selecting the individual cells in Interface Builder and using the inspector to change the cell background to a color or nothing for transparent.
Try [cell setOpaque:FALSE] and [cell setBackgroundColor:[UIColor clearColor]] in cellForRowAtIndexPath:
I would like to create a TRANSLUCENT grouped table view cell. In other words I want to see the grouped table view background pattern, but i don't want completely clear cells. I've seen a lot of questions about transparent cells but none address making translucent (only partially transparent) cells.
This is what I'm trying:
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.contentView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.5f];
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView.backgroundColor = [UIColor clearColor];
}
And this is the result:
It's almost right, except that the contentView of the cell extends beyond the rounded corners of the grouped cell.
SOLVED by using a transparent image and setting the cell's background view. Would have still liked to do it programmatically though, so if anyone has a solution, I will gladly accept it.
SOLVED Part II Can also be done by setting backgroundView to a new UIView, that has the desired background color and rounded corners via QuartzCore's setCornerRadius call to the view's layer property.
This should be all you need:
cell.contentView.backgroundColor = [UIColor clearColor];
cell.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
For anyone else wondering, the code that I ended up using was:
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...
cell.contentView.backgroundColor = [UIColor clearColor];
cell.backgroundColor = [UIColor clearColor];
UIView *bgView = [[UIView alloc] init];
[[bgView layer] setCornerRadius:10.0f];
[bgView setBackgroundColor:[UIColor colorWithWhite:1.0f alpha:0.25f]];
cell.backgroundView = bgView;
//...
return cell;
}