UITableView backgroundColor always white on iPad

后端 未结 16 1514
生来不讨喜
生来不讨喜 2020-12-07 21:35

I\'m working on a project. I have plenty of UITableViews which are set as clear color. Their views\' background color are set to my custom color and everything

相关标签:
16条回答
  • 2020-12-07 22:27

    SWIFT I had this problem too. Works fine on .phone but tableViewCells go white on .pad. Thought I'd show how I fixed it using swift.

    Connect The tableViewCell to the viewController.swift as an @IBOutlet like so:

        @IBOutlet weak var tvc1: UITableViewCell!
        @IBOutlet weak var tvc2: UITableViewCell!
        @IBOutlet weak var tvc3: UITableViewCell!
    

    Then in viewDidLoad put the following:

    tvc1.backgroundColor = tvc1.backgroundColor
    tvc2.backgroundColor = tvc2.backgroundColor
    tvc3.backgroundColor = tvc3.backgroundColor
    

    Very strange, I don't know whats happening here but this solved it for me.

    0 讨论(0)
  • 2020-12-07 22:28

    Good News: According to the release notes, for iOS 10:

    When running on iPad, the background color set for a UITableViewCell in a Storyboard is now respected.

    For versions <10:

    I was seeing this in iOS 8 (8.3). Even though in IB my cells were "clear color" and their content views were "clear color" they would render as white. An imperfect but reasonable solution, since it still takes values from IB:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        ...
        cell.backgroundColor = cell.contentView.backgroundColor;
        return cell;
    }
    

    It seems that my dequeued reuseable cells get their background forced to white on iPad. I was able to determine this using the view hierarchy debugger.

    Once I did this I was able to use the table's background color and didn't have to set a background view, although that works as well.

    0 讨论(0)
  • 2020-12-07 22:28

    This can be achieved in a Storyboard as follows:

    • Show the document outline for your storyboard
    • Within your table, pick a TableViewCell
    • go to the Content View within that Cell
    • Set the background colour of the Content view.

    Result: iPad and iPhone simulator views look the same

    0 讨论(0)
  • 2020-12-07 22:28

    This seems to be fixed with iOS 10 Beta 4 as mentioned in release notes under UIKit notes:

    0 讨论(0)
  • 2020-12-07 22:31

    Swift 5

    I have a table view with many different cells inside, each one has different color.

    The answer from @Ben Flynn cell.backgroundColor = self.contentView.backgroundColor can not achieve that. The reason is self.contentView.backgroundColor is nil, so what you did is just clear the cell.backgroundColor = nil.

    Basically it is the bug from Xcode (I think, yeah it sucks!), cell.backgroundColor still has color, but it can not display.

    After debug for a while, based on @Ray W answer, here is my solution.

    class YourCustomCell: UICollectionViewCell {
        override func awakeFromNib() {
            super.awakeFromNib()
            backgroundColor = backgroundColor // Tricky to re-apply the background color
        }
    }
    
    0 讨论(0)
  • 2020-12-07 22:34

    SWIFT 3.XX

    Put this

    UITableViewCell.appearance().backgroundColor = UIColor.clear
    

    In AppDelegate

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
    
    0 讨论(0)
提交回复
热议问题