UITableView backgroundColor always gray on iPad

前端 未结 9 1414
小蘑菇
小蘑菇 2020-11-30 18:02

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 lig

相关标签:
9条回答
  • 2020-11-30 18:39

    Try one of these.

    [myTableView setBackgroundView:nil];
    [myTableView setBackgroundView:[[[UIView alloc] init] autorelease]];
    
    0 讨论(0)
  • 2020-11-30 18:39

    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.

    0 讨论(0)
  • 2020-11-30 18:40

    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];
    }
    
    0 讨论(0)
  • 2020-11-30 18:49

    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];
    }
    
    0 讨论(0)
  • 2020-11-30 18:50

    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];
    
    0 讨论(0)
  • 2020-11-30 18:53

    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
    
    0 讨论(0)
提交回复
热议问题