iOS 8 UITableView background color appearance - ios

Xcode 6 beta 6, trying to change all UITableView's background colours in appearance proxy:
[[UITableView appearance] setBackgroundColor:[UIColor redColor]]
But seems that it doesn't work.
Steps to reproduce:
1 Create single view project
2 Add UITableView to ViewController in storyboard
3 Set delegates to view controller and change background in IB:
4 Add dynamic cell and configure data source:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"Cell1Identifier" forIndexPath:indexPath];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60.f;
}
5 In app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[UITableView appearance] setBackgroundColor:[UIColor redColor]];
return YES;
}
6 Run app and watch incorrect colour:
Any suggestions how to fix it?
Setting background color for every table doesn't look like good solution.

try to "reset" the BackgroundColor to "Default" in the InterfaceBuilder (even if its already Default, you'll see a little color change)
this doesn't works with grouped style tableviews
UPDATE:
this worked for me
[[UIScrollView appearance] setBackgroundColor:[UIColor redColor]];

Clear color for TableViewCell not working in Tablet in XCode 6. The following workaround solve for me.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}

It appears that for iOS 8, at least for our circumstances, when we set a view's background color with a table in that view, we have to explicitly set the table and cell background colors to Clear in order for the desired color from the view to show. I suppose, with prior iOS versions, the cells were defaulted to transparent but that seems no longer the case.

Set cell background color to transparent in
tableView:willDisplayCell:forRowAtIndexPath:

[UITableVIew backgroundColor] is not marked with UI_APPEARANCE_SELECTOR. Appearance proxies will only work if selector is marked with UI_APPEARANCE_SELECTOR.

It seems that the appearance proxy exhibits a seemingly unreported bug in iOS 8.0.x; it's unrelated to the iOS 7 change involving the default background colour of table cells.
The relative good news is that this only seems to be an issue between iOS 8.0 and 8.0.2, because:
My iPhone 4S on 7.1.2 doesn't have this issue,
My colleague's iPhone 6 running 8.1 doesn't have this issue.
Until most users have upgraded you will have to set the background explicitly on the object itself, sending us back into the pre-appearance era :(

Related

UITableView - transparent background in iOS 7

I am checking my app with ios 7 beta. I have set background color of UITableView to clear color. Its still showing white background.
Is there any other way around to make it transparant?
I couldn't make it work but the following worked perfectly :
-(void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
Setting tableview backgroundColor to clear color working absolutely fine in iOS 7 chk screenshot see those separator lines for the table cells while the tableview is transparent.
Believe it or not, the following statement, added to cellForRowAtIndexPath, will cause iOS 7 to honor the XIB-specified color:
cell.backgroundColor = cell.backgroundColor;
In my XCode 5 Storyboard I had to set the background color of the 'View' section on the Table View properties to clear as well.

Changing background color of selected UITableViewCells in iOS5

My app displays grouped static UITableViews in different screens. Is there anyway of using the appearance proxies
[UITableView appearance]
or
[UITableViewCell appearance]
to customise the background color for the selected cells? Basically I would need to modify
cell.selectedBackgroundView.backgroundColor
for each cell in my app,but I cannot find the right property to be set in the proxy object.
BTW I have tried also the normal approach (being a group static table):
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor];
}
but it doesn't work. Any suggestion?
You need to provide a view to selectedBackgroundView. There isn't one already there. Try:
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds] ;
cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor] ;
Also, a better place to put this code (at least the first line) is in -tableView:cellForRowAtIndexPath: when the cell is created. Otherwise you will be creating a new background view every time the cell is displayed.

UITableViewCell background color is different in iOS5 vs iOS4

The grouped UITableViewCell uses tableCellGroupedBackgroundColor color instead of regular whiteColor color, in iOS 5. As a result, the background of your custom cells doesn't match with those of UITableViewCell (Default, Subtitle, Value1, Value2 style cells).
What's the best way to make sure that same background color is used in custom UITableViewCell and default UITableViewCell (and associated UILabel and other elements) - w.r.t. both iOS4 and iOS5?
Note: In the Editor i can see a new color by the name of tableCellGroupedBackgroundColor, but there's no category/method available to get this color programmatically.
Edit:
You can use following technique to change the background color of controls on your cell, but in-case of custom cells, how can you set appropriate background color based on the OS (iOS 4 vs iOS 5)?
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
for (UIView* view in cell.contentView.subviews) {
view.backgroundColor = cell.backgroundColor;
}
}
Easiest solution (my current implemented):
This just makes sure that all my cells have white background, irrespective of the OS. Somehow, i don't need to apply white color to cell.contentView.subviews.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor:[UIColor whiteColor]];
}
Here's one quick-and-dirty way to do it, presuming we're dealing with a solid background color, no gradients or anything like that.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
for (UIView *subview in [cell.contentView subviews]) {
subview.backgroundColor = cell.backgroundColor;
}
}
On iOS 5, it can help avoid unsightly view lines. :)
Alternatively you could set the backgroundColor of your subviews (labels etc) to [UIColor clearColor]
If you subclass UITableViewCell you could also try overriding setBackgroundColor:
- (void)setBackgroundColor:(UIColor *)backgroundColor {
[super setBackgroundColor:backgroundColor];
[[self myLabel] setBackgroundColor:backgroundColor];
}
It appears that this method is used by iOS when it sets the default background colour for the cell.

Different cell bg color depending on iOS version (4.0 to 5.0)

I have a custom grouped UITableViewCell, with a couple of UILabels on it. Since the UITableViewCell background color used to be pure white, it matched the UILabels' default background color, so the UILabel box wasn't visible..
After updating to iOS 5.0, I notice that now the default background color for grouped UITableViewCells is a more greyish white (actually #f7f7f7), and as a consequence the UILabels' frame is visible in an ugly way..
So, what is the best way to set the UILabels' background color when it needs to vary between different iOS versions? I know I could use opaque = NO and [UIColor clearColor], but I would prefer to paint the UILabels' background for performance reasons.
In the delegate method tableView:willDisplayCell:, the UITableViewCell will have the background colour set to white or, in iOS 5, greyish.
You can change all your the backgroundColor of all your subviews.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
for (UIView* view in cell.contentView.subviews) {
view.backgroundColor = cell.backgroundColor;
}
}
Nothing wrong with gcamp's answer, but if you'd prefer to keep the background white on both iOS4 and iOS5, then just do this:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor:[UIColor whiteColor]];
}
I saw that problem in my custom UITableViewCell in a grouped TableView I just set the label background color to clear and that fixed it for me and works in iOS4 and iOS5. I set this in the custom cell code where the UILabel gets created as below:
[nameLabel setBackgroundColor:[UIColor clearColor]];
After that problem gone.
You can call:
[[UIDevice currentDevice] systemVersion]
Although that is discouraged for a lot of reasons, and I'm not sure it's the best way to solve your problem. In principle, you should be able to deploy view code and get the consistent results you want by some other means.
If you do actually want to compare device versions, you'd probably set a property and check the device's OS when the view controller loads, as opposed to within your cellForRowAtIndexPath...
What I did was set the background color of the cell to [UIColor whiteColor] in willdisplaycell...
That way I control how things look.
its use UIColor.systemBackground but its available for IOS 13 Only
so use it likes this :
if #available(iOS 13.0, *) {
cell.backgroundColor = UIColor.systemBackground
} else {
cell.backgroundColor = .white
}

Why is there a background on a table cell's text, but only on the iPad?

I'm trying to set the background color on a cell. It works fine in iOS 4 on a 3GS and in the sim, but when I test in 3.2 I get a background on the text label that I can't get rid of.
I've tried to set the opacity and background of the label, detail label, content view, and accessory views manually and had no success. I even resorted to the following to make sure I "got everything" (though I don't know what else there would be...).
void setBackgroundColor (UIView *view, UIColor *color) {
view.opaque = NO;
view.backgroundColor = color;
for (UIView *subview in [view subviews]) {
setBackgroundColor(subview, color);
}
}
...
setBackgroundColor(cell, [UIColor blueColor]);
So, what could cause this on the iPad but not on the iPhone?
It turns out that you can't set the background in cellForIndexPath and have to do it in:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
Now it works perfectly.

Resources