ios UITableView 清除背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18753411/
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-31 01:06:58  来源:igfitidea点击:

UITableView clear background

iosuitableview

提问by Teddy13

I realize that iOS 7 has not officially been released and we should not discuss it BUT I am going crazy trying to figure out this problem. On iOS 6, my table view was transparent and looked great. First time running iOS 7, and the background is white.

我意识到 iOS 7 还没有正式发布,我们不应该讨论它,但我想弄清楚这个问题我快疯了。在 iOS 6 上,我的表格视图是透明的,看起来很棒。第一次运行iOS 7,背景为白色。

I have tried making the table backgroundColor, cell color etc etc to UIColor clearColor but have no change.

我曾尝试将表格背景颜色、单元格颜色等设置为 UIColor clearColor 但没有任何变化。

How to fix this problem?

如何解决这个问题?

the table

桌子

回答by slamor

    // Fix for iOS 7 to clear backgroundColor
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView = [[UIView new] autorelease];
    cell.selectedBackgroundView = [[UIView new] autorelease];

in cellForRowAtIndexPath

在 cellForRowAtIndexPath 中

Also, make sure that your tableview actually has transparent background (in storyboard): enter image description here

另外,请确保您的 tableview 实际上具有透明背景(在故事板中): 在此处输入图片说明

回答by Maximus Vermillion

Put this:

把这个:

cell.backgroundColor = [UIColor clearColor];

In this section:

在这个部分:

cellForRowAtIndexPath

回答by dandan

Try setting backgroundView to nil first.

首先尝试将 backgroundView 设置为 nil。

[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor clearColor]];

Not sure if this is a change in documentation with iOS7 or has always been there and just did not affect background color, but per UITableView Class Reference @property backgroundView

不确定这是 iOS7 文档中的更改还是一直存在并且没有影响背景颜色,但根据UITableView 类参考 @property backgroundView

"You must set this property to nil to set the background color of the table view."

“您必须将此属性设置为 nil 才能设置表格视图的背景颜色。”

edit: corrected code syntax

编辑:更正的代码语法

回答by nab

This has been answered, but incorrectly in a lot of ways.

这已经得到了回答,但在很多方面都不正确。

You need to implement the below delegate method:

您需要实现以下委托方法:

    - (void)tableView:(UITableView *)tableView  willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
}

You can't put the fix in cellForRowAtIndexPath because that is after the cell is rendered, and it will flash a white background before the background gets set to clear (on slower devices).

您不能将修复程序放在 cellForRowAtIndexPath 中,因为那是在单元格呈现之后,并且在背景设置为清除(在较慢的设备上)之前,它会闪烁白色背景。

Use this delegate method and your problems are solved!

使用这个委托方法,你的问题就解决了!

回答by asdusd

Actually the officially correct place to change cell background color is different according to documentation (UITableViewCell Class Reference):

实际上,根据文档(UITableViewCell Class Reference),更改单元格背景颜色的官方正确位置是不同的:

Whether you use a predefined or custom cell, you can change the cell's background using the backgroundView property or by changing the inherited backgroundColor property. In iOS 7, cells have a white background by default; in earlier versions of iOS, cells inherit the background color of the enclosing table view. If you want to change the background color of a cell, do so in the tableView:willDisplayCell:forRowAtIndexPath: method of your table view delegate.

无论您使用预定义的单元格还是自定义单元格,您都可以使用 backgroundView 属性或通过更改继承的 backgroundColor 属性来更改单元格的背景。在 iOS 7 中,单元格默认为白色背景;在早期版本的 iOS 中,单元格继承了封闭表格视图的背景颜色。如果要更改单元格的背景颜色,请在表视图委托的 tableView:willDisplayCell:forRowAtIndexPath: 方法中进行。

回答by Jamal alayq

swift 3, 4 and 5

快速 3、4 和 5

cell.backgroundColor = UIColor.clear

回答by Berk

This is quite a frustrating problem. Here's my current solution:

这是一个相当令人沮丧的问题。这是我目前的解决方案:

Add this to your UITableViewCellsubclass.

将此添加到您的UITableViewCell子类中。

- (void)didMoveToSuperview {
    [super didMoveToSuperview];

    self.backgroundColor = [UIColor clearColor];
}

回答by DevC

This worked for me in iOS7+:

这在 iOS7+ 中对我有用:

self.tableView.backgroundColor =[UIColor blueColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = nil;`

and then in cellForRowAtIndexPath:

然后在 cellForRowAtIndexPath:

cell.backgroundColor = [UIColor clearColor];

回答by SoliQuiD

This did only work for me when i edited a clear background color for each cell and a clear color for the table itself.. BOTH PROGRAMMATICALLY

当我为每个单元格编辑清晰的背景颜色和表格本身的清晰颜色时,这只对我有用。

to set the clear color for the table:

为表格设置清晰的颜色:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    initMenu()
    myTableView.backgroundColor = UIColor.clearColor()
}

to set the color for the cells:

设置单元格的颜色:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("tablecellid", forIndexPath: indexPath)
    cell.backgroundColor = UIColor.clearColor()
    return cell
}

回答by Marco Antonio Uzcategui Pescoz

try this code snippet

试试这个代码片段

cell.contentView.backgroundColor = [UIColor clearColor];
cell.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];