Causaelity
Causaelity

Reputation: 1866

UITableView / UITableViewCell challenge with transparent background on iPad with iOS7

Last night I decided to upgrade to Xcode 5 and take a look at my current project. After updating my storyboards to the new UI everything looked great and ran fine. Since I have a universal binary, I decided to test things on iPad as well and noticed that a new white background had been introduced into my UITableview where there once used to be a transparent / clear color. This appears to be happening on the cell level, not the table level. When I run things on the 6.1 simulator everything looks fine on iPad & iPhone. And everything looks fine on iPhone for iOS7.

Everything that I have set up for interface builder is identical for iPhone & iPad. From what I can tell it has something to do with this new "content view" (which is a subgroup of the Item Cell) not honoring a transparent value / setting.

Any thoughts / ideas?

Upvotes: 33

Views: 8359

Answers (4)

wils
wils

Reputation: 791

In case anyone else is still having trouble with table view/cell transparency on iPad, this may help (copied from https://stackoverflow.com/a/31396483/2301213 , it's in swift since the times they are a changin')

It seems that somewhere in the process of adding a UITableView to the window (between willMoveToWindow and didMoveToWindow), some iPad's reset the backgroundColor of the table view to white. It does this covertly without using the backgroundColor property.

I now use this as a base class in place of UITableView when I need a colored/transparent table...

class ColorableTableView : UITableView {
    var _backgroundColor:UIColor?
    override var backgroundColor:UIColor? {
        didSet {
            _backgroundColor = backgroundColor
        }
    }
    override func didMoveToWindow() {
        backgroundColor = _backgroundColor
        super.didMoveToWindow()
    }
}

Cells also have their backgroundColor's set to white on my iPad in the same way (i.e. those that are in the table during the move to the window), so the same applies to them, lest you end up with the odd opaque cell popping up from time to time as it is reused ...

class ColorableTableViewCell : UITableViewCell {
    var _backgroundColor:UIColor?
    override var backgroundColor:UIColor? {
        didSet {
            _backgroundColor = backgroundColor
        }
    }
    override func didMoveToWindow() {
        backgroundColor = _backgroundColor
        super.didMoveToWindow()
    }
}

Upvotes: 3

Ace Green
Ace Green

Reputation: 391

If you are using a static cell table. you can do the following:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

     cell.backgroundColor = UIColor.clearColor()
}

Upvotes: 0

user3164248
user3164248

Reputation: 155

If you are using a custom UITableViewCell and invoking it from storyboard/xib you can use the following code.

@implementation YourCustomTableViewCell

- (void) awakeFromNib
{
    self.backgroundColor = [UIColor clearColor];
}

Upvotes: 4

Causaelity
Causaelity

Reputation: 1866

After wasting multiple hours with interface builder, I'm thinking that there might be a bug there. So I started to look for a programatic answer. Apparently had I started here I could have saved a ton of time. By adding to the method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

I was able to solve the transparency issue on iPad by adding this one line:

cell.backgroundColor = [UIColor clearColor];  // Adding this fixes the issue for iPad

Hope this helps everyone else with the white background seen for ipad with tables and iOS7!

Upvotes: 103

Related Questions