ios Objective C:如何使 UITableView 的背景颜色一致

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6053160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:00:04  来源:igfitidea点击:

Objective C: How to make background color of UITableView Consistent

objective-ciosuitableview

提问by Zhen

I have been trying to set the background color of my table view but am facing an issue

我一直在尝试设置表格视图的背景颜色,但遇到了一个问题

This is what I am trying to do.

这就是我想要做的。

//Set background color of table view (translucent)
self.tableView.backgroundColor = [UIColor colorWithRed:0.0 green:0.2 blue:0.5 alpha:0.7];

//Set frame for tableview
[self.tableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.picker.frame.size.height)];

After setting the table cells, I saw that there is an inconsistency in the alpha level around the table view cell (see screenshot below)

设置表格单元格后,我看到表格视图单元格周围的alpha级别存在不一致(见下面的截图)

tableView BackgroundColor

表视图背景颜色

Is there anyway to make the color / alpha level consistent for the background? (Note: I am not setting a background image, only color and alpha level)

有没有办法使背景的颜色/阿尔法级别保持一致?(注意:我没有设置背景图片,只有颜色和 alpha 级别)

Thanks!

谢谢!

Zhen Hoe

振和

回答by ACBurk

I recently ran into this problem myself and think I found the solution

我最近自己遇到了这个问题,并认为我找到了解决方案

self.tableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundColor = [UIColor colorWithRed:0.0 green:0.2 blue:0.5 alpha:0.7];

Give that a shot, might need some massaging for your specific case (looks like you have some picture behind that translucent blue too).

试一试,可能需要针对您的特定情况进行一些按摩(看起来您在半透明蓝色后面也有一些图片)。

回答by WrightsCS

You need to make the background of the UITableView clear as well as set Opaque to FALSE

您需要使 UITableView 的背景清晰,并将 Opaque 设置为 FALSE

[self.tableView setBackgroundColor: [UIColor clearColor]];
[self.tableView setOpaque: NO];

回答by Gamma-Point

All you need to do are 2 things:

所有你需要做的是两两件事:

E.g. A view has a table view in it. In the XIBfile set the background of the UITableViewto clear color. Set the background of the container view of the table view to the image you want.

例如,视图中有一个表视图。在XIB文件中将背景设置UITableView为清除颜色。将table view的container view的背景设置成你想要的图片。

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"app_background.png"]];

If the ViewController is actually a TableViewControllerthen set:

如果 ViewController 实际上是一个TableViewControllerthen 设置:

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"app_background.png"]];

You can set the row background with inside the table view delegate (the view or tableview controller)

您可以在表视图委托(视图或表视图控制器)内部设置行背景

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

if ([indexPath row] % 2 == 0) {//248 G:192 B:100


[cell setBackgroundColor: [UIColor redColor]];
} else {
   [cell setBackgroundColor: [UIColor colorWithPatternImage:[UIImage imageNamed:@"xyz.png"]]];

  }
}