在视图中的TableView,我如何使它透明

我有一个表视图embedded到主视图,它是我可以使用静态表的唯一方法。 主视图有一个固定的背景颜色,embedded式视图不隐藏,它不透明,有没有办法做到这一点?

为一个视图声明两次颜色似乎很愚蠢

试试这个代码:

self.view.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.35]; 

除了将表视图的背景颜色设置为[UIColor clearColor] ,还可能需要实现此委托方法:

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

我不得不这样做,因为单元格从cellForRowAtIndexPath返回后设置它们的背景颜色。

您可以使用setAlpha属性根据您需要的透明度来指定范围从0.0到1.0的浮点值,例如:

 [self.tableView setAlpha:0.5]; 

为迅速:

 override func viewDidLoad() { super.viewDidLoad() tableView.backgroundColor = .clearColor() } 

 func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath){ cell.backgroundColor = tableView.backgroundColor cell.contentView.backgroundColor = tableView.backgroundColor } 

否则,你可以设置背景颜色清除颜色和alpha到一个