6

如何使tableHeaderView的背景 清晰,但保持 UITableView 背景的其余部分不透明

我正在使用透明的 tableHeaderView 来实现视差效果。tableView 后面的对象比清除 tableHeaderView“窗口”长,因此我可以将可见数据居中。这适用于较长的列表,因为我可以使用 tableView 作为掩码,但是当表格中没有足够的单元格时,背景对象会显示在单元格下方。

相关代码:

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

UIView *tableHeaderView = [[UIView alloc] initWithFrame: CGRectMake(0.0, 0.0, 320.0, 250.0)];
tableHeaderView.backgroundColor = [UIColor clearColor];
self.tableView.tableHeaderView = tableHeaderView;

我尝试为 tableView 设置背景颜色,但这会使整个 UITableView 变得不透明(包括 tableHeaderView),从而删除了我在顶部的“窗口”。

关于如何在将 UITableView 的主体设置为不透明的同时保持透明 tableHeaderView 的任何想法?

谢谢!

4

5 回答 5

7

为多个部分做公认答案的更简单方法

// Set the view for each cell with a clear color
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)]; //
    view.backgroundColor = [UIColor clearColor];
    return view;
}

// Set the height of your desired header
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
于 2014-11-12T07:01:12.273 回答
6

几天后,我能够弄清楚。解决方案的前提是在表格的背景视图中添加一个子视图,并在滚动时更改子视图的框架。

viewDidLoad 中的相关代码:

...
// Create the UIView that will become the tableView backgroundView
UIView *tableViewBackground = [[UIView alloc] initWithFrame:self.tableView.frame];
tableViewBackground.backgroundColor = [UIColor clearColor];

// Create the opaque backgroundView and set the frame so that it starts below the headerView
partialBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 250, 320.0, self.view.frame.size.height)];
partialBackgroundView.backgroundColor = [UIColor redColor];

// Add the partial background to the main background view and apply it to the tableView
[tableViewBackground addSubview:solidTableBodyBackgroundView];
self.tableView.backgroundView = tableViewBackground;
...

然后在滚动时,可以更新 scrollViewDidScroll 中的“可见窗口”:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat scrollOffset = scrollView.contentOffset.y;
    partialBackgroundView.frame = CGRectMake(0, 250 - scrollOffset, 320, self.view.frame.size.height);
    // Other parallax code for scrolling
}

可能有更好的方法可以做到这一点,但我发现这很简单,而且效果很好。

于 2013-11-26T22:00:34.493 回答
1

如果像我一样在委托方法中返回UITableViewHeaderFooterView作为标题视图的子类,那么这是一个非常晚但快速而简单的答案。viewForHeaderInSection

设置myHeaderView.backgroundColor = UIColor.clear对你没有帮助?

尝试设置myHeaderView.layer.backgroundColor = UIColor.clear.cgColor

于 2018-10-08T13:42:41.370 回答
1

Swift-5,透明部分标题

  1. 添加视图并将其背景设置为透明
  2. ViewForSectionHeader然后在 tableView的方法中返回这个视图
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return viewForSection
}
于 2020-02-10T10:21:46.077 回答
0

除非我误解了某些东西,否则您应该可以分两步执行此操作:

  1. 您需要使表格中的单元格不透明。将 tableView 的背景颜色设置为 [UIColor clearColor],表格标题的视图(如果适用,还有节标题)的视图也是如此。

  2. 然后获取您的表格页脚视图(tableFooterView的属性UITableView),使其不透明,并使其非常高。当表格中只有几个单元格时,表格页脚将占据屏幕的其余部分。

再说一次,我可能会误解一些东西,但试一试,看看你得到了什么。祝你好运!

于 2013-11-22T22:26:45.997 回答