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!


    尝试其中之一。

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

    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
    

    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];
    }
    
    链接地址: http://www.djcxy.com/p/95698.html

    上一篇: 将iPhone应用转换为iPad XIB文件问题

    下一篇: UITableView backgroundColor在iPad上始终为灰色