UITableView backgroundColor always gray on iPad

空扰寡人 提交于 2019-11-26 11:51:08

问题


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:

  1. Create a new navigation-based project.
  2. Open RootViewController.xib and set the table view style to \"Grouped\".
  3. Add this responder to the RootViewController:

    - (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    }
  4. Select Simulator SDK 3.2, build and run.
  5. You will get a black background (device and simulator).
  6. Select your target in the project tree.
  7. Click on Project : Upgrade Current Target for iPad.
  8. Build and run.
  9. You will get a light gray background.
  10. Revert the table view style to Plain and you will get a black background.

Thanks for your help!


回答1:


Try one of these.

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



回答2:


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



回答3:


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];
}



回答4:


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.




回答5:


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;

}




回答6:


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.




回答7:


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];



回答8:


  • 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.




回答9:


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];
}


来源:https://stackoverflow.com/questions/2688007/uitableview-backgroundcolor-always-gray-on-ipad

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