代码之家  ›  专栏  ›  技术社区  ›  rjobidon

UITableView背景色在iPad上始终为灰色

  •  128
  • rjobidon  · 技术社区  · 16 年前

    当我设置 backgroundColor UITableView groupTableViewBackgroundColor

    复制步骤:

    1. 打开RootViewController.xib并将表视图样式设置为“Grouped”。
    2. 将此响应程序添加到RootViewController:

      - (void)viewDidLoad {
      [super viewDidLoad];
      self.view.backgroundColor = [UIColor blackColor];
      }
    3. 在项目树中选择目标。
    4. 构建并运行。

    谢谢你的帮助!

    9 回复  |  直到 13 年前
        1
  •  245
  •   Wayne Hartman    14 年前

    试试这个。

    [myTableView setBackgroundView:nil];
    [myTableView setBackgroundView:[[[UIView alloc] init] autorelease]];
    
        2
  •  30
  •   Alexander    16 年前

    非常感谢您的解决方案。 我将此应用于UIViewController中带有IBOutlet的UITableView属性,其工作原理如下:

    [panelTable setBackgroundView:nil];
    [panelTable setBackgroundView:[[[UIView alloc] init] autorelease]];
    [panelTable setBackgroundColor:UIColor.clearColor]; // Make the table view transparent
    
        3
  •  6
  •   Philipp Frischmuth    14 年前

    backgroundView 属性似乎用于为分组表创建灰色背景色。因此,在iPad上改变分组表的背景色应该 nil 出去 属性,然后设置 backgroundColor

    - (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
  •  2
  •   WiseGuy    11 年前

    我认为值得一提的是,从Xcode6.1和iOS8.1开始,特别是对于iPad(如果你想设置单元格背景),似乎你必须设置表格背景和单元格背景。

    例如,在iPhone情节提要上,可以将单元格设置为“清除颜色”,然后以编程方式为具有背景图像的透明表设置表的背景图像。然而,如果你在iPad上查看同样的配置,电池就不清楚了。对于iPad,需要将单元格设置为以编程方式清除。

        5
  •  1
  •   cube108    12 年前

    在iOS 7.1 4/2014上,使用手机本身的backgroundColor为我解决了这个问题

    - (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
  •  0
  •   gmogames    14 年前

    [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
    

    注意TableView alloc没有ARC的自动释放。

        7
  •  0
  •   irmakoz    14 年前

    如果要将UIViewController添加到另一个UIViewController,除了UITableView之外,还需要将视图的背景设置为clearColor,否则将获得白色背景而不是浅灰色。

    if ([myViewController.myTableView respondsToSelector:@selector(setBackgroundView:)]) {
        [myViewController.myTableView setBackgroundView:nil];
    }
    myViewController.myTableView.backgroundColor = [UIColor clearColor];
    myViewController.view.backgroundColor = [UIColor clearColor];
    
        8
  •  0
  •   QED    11 年前
    • 运行iOS 7.1到8.3

    以上这些都不适用于我的UIViewController->UITableView使用单个XIB指定。所做的工作是将整个设置移动到一个故事板中,并使用IB检查器设置背景色。

        9
  •  0
  •   elegance66    7 年前

    UITableView 背景颜色将始终为 groupTableViewBackgroundColor

    症状就是这个问题- UITableView is resetting its background color before view appears

    在我把背景色调好之后 init 功能,它是正确的。在 viewDidLoad 和 viewWillAppear 阶段,这是正确的。然而,在 viewDidAppear

    公认的答案现在行不通。

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

    只需设置tableView的背景色 视图加载 功能 而不是 或

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