【问题标题】:UITableView background colorUITableView 背景颜色
【发布时间】:2011-10-07 03:27:27
【问题描述】:

我看过各种关于 iOS 4.2 的 UITableView 背景颜色的帖子,但我似乎无法更改背景颜色。

UIView *bgView = [[[UIView alloc] init] autorelease];
bgView.backgroundColor = [UIColor blackColor];
bgView.opaque = YES;
self.myTableView.backgroundView = bgView;

我已将此代码放在 viewDidLoad 中,但它不会改变颜色。我的 UITableView 是带有 UITableViewDelegate 和 UITableViewDataSource 的 UIViewController 内的 IBOutlet,所以我在 viewDidLoad 处考虑 UITableView 已经渲染,我无法更改其背景颜色。

EDIT * - UITableView 被分组

【问题讨论】:

    标签: ipad uitableview


    【解决方案1】:

    尝试将表格视图的背景视图设置为 nil,同时设置表格视图的背景颜色

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.myTableView.backgroundView = nil;
        self.myTableView.backgroundColor = [UIColor blackColor];
    }
    

    【讨论】:

    • 注意 self.myTableView.backgroundView = nil;让后台在 iOS 5+ 的 iPad 上运行至关重要
    【解决方案2】:

    只需更改 tableView 和 tableView 的 backgroundView 的 backgroundColor 属性。

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

    【讨论】:

      【解决方案3】:

      确认适用于所有 iOS
      如果您没有在 .h 中声明 myTableView,请在 viewDidLoad 中使用以下代码

      self.tableView.backgroundView = nil;
      self.tableView.backgroundColor = [UIColor greenColor];
      

      如果您想将图案加载为背景,请添加以下行

      self.tableView.opaque = NO;
      self.tableView.backgroundColor = [UIColor clearColor];
      self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"myImage"]];
      

      【讨论】:

        【解决方案4】:

        0 x 0 像素大的背景视图将不可见。试试 initWithFrame: 给视图一个实际的大小。

        【讨论】:

          【解决方案5】:

          使用这个委托来改变颜色

          - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
              cell.backgroundColor = [UIColor redColor];
          }
          

          编辑 1:

          UIView* backgroundView = [ [ [ UIView alloc ] initWithFrame:CGRectZero ] autorelease ];
          backgroundView.backgroundColor = [ UIColor yellowColor ];
          cell.backgroundView = backgroundView;
          for ( UIView* view in cell.contentView.subviews ) 
          {
              view.backgroundColor = [ UIColor clearColor ];
          }
          

          【讨论】:

          • 这只是在 uitableview 本身之后更改单元格背景颜色。
          • 不完全是,它只是更新了那个特定的单元格。所以单元格将是那种特定的颜色,但背景仍然是灰色的。也许我忘了添加一个重要的细节,那就是 tableview 是分组的(我的错)。如果不分组,它肯定会起作用
          【解决方案6】:

          只是添加到这个。我需要更改 tableview 的 uiscrollview 背景颜色以获得特定效果。您可以在全局范围内执行此操作:

          [[UIScrollView appearance] setBackgroundColor:[UIColor whiteColor]];
          

          或将其限制在单个控制器(iOS9):

          [[UIScrollView appearanceWhenContainedInInstancesOfClasses:@[[MyViewController class]]] setBackgroundColor:[UIColor whiteColor]];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-09-04
            • 2015-11-01
            • 1970-01-01
            • 1970-01-01
            • 2011-09-12
            • 2011-03-25
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多