ChatGPT解决这个技术问题 Extra ChatGPT

UITableView backgroundColor always gray on iPad

When I set the backgroundColor for my UITableView it works fine on iPhone (device and simulator) but NOT on the iPad simulator. Instead I get a light gray background for any color I set including groupTableViewBackgroundColor.

Steps to reproduce:

Create a new navigation-based project. Open RootViewController.xib and set the table view style to "Grouped". Add this responder to the RootViewController: - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; } Select Simulator SDK 3.2, build and run. You will get a black background (device and simulator). Select your target in the project tree. Click on Project : Upgrade Current Target for iPad. Build and run. You will get a light gray background. Revert the table view style to Plain and you will get a black background.

Thanks for your help!

Ah, now iPhone and iPad aligned again here in iOS6 :-)
Bravo for using the seldom-used terms "Steps to reproduce"! :-p

W
Wayne Hartman

Try one of these.

[myTableView setBackgroundView:nil];
[myTableView setBackgroundView:[[[UIView alloc] init] autorelease]];

I don't know if there are side effects but it works! Thanks! self.tableView.backgroundView = nil;
VERY VERY IMPORTANT: This works only with SDK 3.2. For backward compatibility with 3.1.3. and earlier you must check if the table view responds to the backgroundView property: if ([self.tableView respondsToSelector:@selector(backgroundView)]) self.tableView.backgroundView = nil; otherwise you app will crash and exit abruptly, you can trust me!
yes, I tried the above solution and it worked for me. Can anyone tell me what is this issue actually. Why when we run the app, it is greyed out?
Please someone tell us why this is required to solve this problem?
because clearly the new ios lets your put a background view. Why its set to some gray view is just an issue that Apple introduced. very odd, very dumb. But then again its every day that I encounter something odd and dumb while coding for iOS
A
Alexander

Thanks a lot for this solution. I applied this on a UITableView property with IBOutlet in a UIViewController and it works well like:

[panelTable setBackgroundView:nil];
[panelTable setBackgroundView:[[[UIView alloc] init] autorelease]];
[panelTable setBackgroundColor:UIColor.clearColor]; // Make the table view transparent

there is not any need for the middle line [panelTable setBackgroundView:[[[UIView alloc] init] autorelease]];
Freezing Fire: actually there is. Without it, you get weird drawing artifacts in the table cell borders.
P
Philipp Frischmuth

On iPad the backgroundView property seems to be used to create the gray background color for grouped tables. So for changing the background color for grouped tables on iPad one should nil out the backgroundView property and then set the backgroundColor on the desired table view.

- (void)viewDidLoad
{
    [super viewDidLoad];

    // If you need to support iOS < 3.2, check for the availability of the
    // backgroundView property.
    if ([self.tableView respondsToSelector:@selector(setBackgroundView:)]) {
        self.tableView.backgroundView = nil;
    }
    self.tableView.backgroundColor = [UIColor whiteColor];
}

Yes, this check should be added to support earlier iOSes.
W
WiseGuy

I think it is worth noting that as of Xcode 6.1 and iOS 8.1, specifically for iPad (if you want to set cell background as well) it seems that you must set table background AND cell background.

For instance, on an iPhone storyboard you can set a cell to clear color, then set background image of table programmatically for a transparent table with background image. However if you were to view this same configuration on iPad the cells would not be clear. Cells will need to be set to clear programmatically for iPad.


I'm seeing this issue for apps build with Xcode 6.4 and running on iOS 9.2. I set my (static) table view cells' background colors to clear in Interface Builder, and they appeared clear on iPhone, but on iPad they were white until I set the cells' background colors to clear in my view controller's -viewDidLoad: method.
c
cube108

tried setting backgroundColor and View for the table, had no luck (Xcode 5 iOS7.1 iPad simulator), looked OK for iPhone, but light grey background color on iPad...

working with backgroundColor for the cell itself fixed this for me on iOS 7.1 4/2014

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

        static NSString *CellIdentifier = @"ViewSomeTriggerCell";

        // get a cell or a recycled cell
        sictVCViewSomeTriggerCell *cellTrigger = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        // put a picture and a word in the cell (positions set in .xib)
        NSString * tname = [self.fetchedTriggersArray objectAtIndex:indexPath.row];
        cellTrigger.imageTrigger.image = [UIImage imageNamed:@"icon_appicon.png"];
        cellTrigger.labelName.text = tname;

        // set background color, defeats iPad wierdness
        // http://stackoverflow.com/questions/2688007/uitableview-backgroundcolor-always-gray-on-ipad

        // clearColor is what I wanted, and it worked, also tested with purpleColor
        cellTrigger.backgroundColor =[UIColor clearColor];
        return cellTrigger;

}


g
gmogames

If your application targets both iPhone and iPad you can do it like this:

[myTableView setBackgroundColor:[UIColor clearColor]]; // this is for iPhone

if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPhone) {
    [myTableView setBackgroundView:nil];
    [myTableView setBackgroundView:[[UIView alloc] init]];
} // this is for iPad only

Notice that the TableView alloc doesn't have autorelease for ARC.


i
irmakoz

If you are adding a UIViewController to another UIViewController you also need to set your view's background to clearColor in addition to UITableView or else you will get a white background instead of light grey.

if ([myViewController.myTableView respondsToSelector:@selector(setBackgroundView:)]) {
    [myViewController.myTableView setBackgroundView:nil];
}
myViewController.myTableView.backgroundColor = [UIColor clearColor];
myViewController.view.backgroundColor = [UIColor clearColor];

Q
QED

Simulating iPad 2

Running iOS 7.1 through 8.3

Built from XCode 6.3

None of the above worked for my UIViewController->UITableView specified using a single XIB. What did work was moving the whole setup into a Storyboard, and setting the background color using the IB inspector.


e
elegance66

I also got this kind of problem. The UITableView background color will be always groupTableViewBackgroundColor no matter what I set.

The symptom is like this issue - UITableView is resetting its background color before view appears

After I set the background color in init function, it is correct. In viewDidLoad and viewWillAppear stage, it is correct. However, in viewDidAppear, the background color is reset to its default color.

The accepted answer does not work now.

[myTableView setBackgroundView:nil];
[myTableView setBackgroundView:[[UIView alloc] init]];

Here is my solution. Just set the tableView's background color in viewDidLoad function rather than init or viewWillAppear.

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor = [UIColor clearColor];
}