Change background of a grouped UITableView

瘦欲@ 提交于 2020-01-10 12:01:38

问题


I'm having some trouble trying to change the background of a UITableView with groups.

_tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tableViewBg.png"]];

This usually works on every other UITableView, but not the one with groups, is there something else I have to do? In IB I have the background color set to a clear color, but that doesn't do much.


回答1:


You additionally need to disable the background view of _tableView:

[_tableView setBackgroundView:nil];
 _tableView.backgroundColor = [UIColor redColor];

No need to add new view to backgroundView. This is working for me in iOS6.




回答2:


why don't you set the tableView.backgroundView? you can alloc an image view withe the specified image and pass it to the background view instead of setting the background color.




回答3:


What I usually do with grouped UITableViews is set the background color to clear, and the set that pattern image to the parents view.

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tableBG.png"]];
self.tableView.backgroundColor = [UIColor clearColor];



回答4:


tableView.backgroundView = nil;
tableView.backgroundColor = [UIColor colorWithPatternImage: xxx];
// tableView.backgroundColor = [UIColor redColor];  // is ok

if you set set the backgroundColor as this, when you scroll the tableView, the backgroundColor view will scroll also. so, you can: tableView.backgroundView = nil; self.view.backgroundColor = ...




回答5:


The selected answer works but it clears the background of both tableHeaderView and table section header view. If you just want table header to be of a certain color say white and section header still to be default grey than do the following -

tableView.tableHeaderView.inputView.backgroundColor = [UIColor whiteColor];



回答6:


Just want to add to Nirav's answer - it can also be done using the iOS 5 appearance proxy.

[[UITableView appearance] setBackgroundView:nil];
[[UITableView appearance] setBackgroundColor:[UIColor lightGreyColor]];

The advantage is that it is applies globally, so you can group all your UI customisations in one place. However, it will apply to all tableViews (not just grouped style).




回答7:


For iOS 9+, changing the background color of the UITableView is enough.

Objective-C

tableView.backgroundColor = [UIColor redColor];

Swift

tableView.backgroundColor = .red



回答8:


Swift 4.2

TO REMOVE BACKGROUND VIEW & COLOR

tableView.backgroundView = nil
tableView.backgroundColor = .clear


来源:https://stackoverflow.com/questions/11047029/change-background-of-a-grouped-uitableview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!