UITableView / UITableViewCell challenge with transparent background on iPad with iOS7

前端 未结 4 1007
死守一世寂寞
死守一世寂寞 2021-01-03 18:25

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

相关标签:
4条回答
  • 2021-01-03 18:41

    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()
        }
    }
    
    0 讨论(0)
  • 2021-01-03 18:57

    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!

    0 讨论(0)
  • 2021-01-03 19:00

    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];
    }
    
    0 讨论(0)
  • 2021-01-03 19:00

    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()
    }
    
    0 讨论(0)
提交回复
热议问题