HotJard
HotJard

Reputation: 4798

iOS 8 UITableView background color appearance

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:

enter image description here

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:

enter image description here

Any suggestions how to fix it? Setting background color for every table doesn't look like good solution.

Upvotes: 20

Views: 18283

Answers (6)

N3al
N3al

Reputation: 311

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

Upvotes: 1

Radin Gospodinov
Radin Gospodinov

Reputation: 2323

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

Upvotes: 1

Thein
Thein

Reputation: 3968

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

Upvotes: 5

Ja͢ck
Ja͢ck

Reputation: 173552

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:

  1. My iPhone 4S on 7.1.2 doesn't have this issue,
  2. 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 :(

Upvotes: 0

Chakalaka
Chakalaka

Reputation: 2827

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

Upvotes: 12

Prethen
Prethen

Reputation: 277

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.

Upvotes: 2

Related Questions