UITableView / UITableViewCell challenge with transparent background on iPad with iOS7

前端 未结 4 1012
死守一世寂寞
死守一世寂寞 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()
        }
    }
    

提交回复
热议问题