【问题标题】:How to set clear background for UITableView Header with opaque body如何为带有不透明主体的 UITableView Header 设置清晰的背景
【发布时间】:2013-12-07 21:20:05
【问题描述】:

如何为 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 的任何想法?

谢谢!

【问题讨论】:

    标签: ios nstableheaderview uitableview


    【解决方案1】:

    除非我有什么误解,否则您应该可以分两步完成:

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

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

    同样,我可能会误解某些内容,但请试一试,看看你会得到什么。祝你好运!

    【讨论】:

    • 当你添加一个 tableFooterView 时,它会扩展你的 tableView 的长度,这样当你滚动时,一切都会离开屏幕的顶部。我想您可以计算单元格底部和屏幕底部之间的间隙,并将页脚高度设置为完全一样,但这似乎是一个 hack。如果我打算以“非理想”方式进行操作,我可能会在背景视图上创建一个蒙版,但保持更新会很痛苦。不过感谢您的建议!
    【解决方案2】:

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

    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
    }
    

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

    【讨论】:

    • 一直在寻找如何解决这个问题,终于让它工作了,谢谢你。
    • 谢谢。这很好用!但是最好使用self.tableView.contentSize作为整个滚动体的partialBackgroundView框架。
    • 很好的解决方案来分离表格视图的“主体”。 @Angolao 的建议绝对是建议的方法。效果很好!
    【解决方案3】:

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

    // 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;
    }
    

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        【解决方案5】:

        Swift-5,透明部分标题

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-25
          • 1970-01-01
          • 2017-08-14
          • 2012-07-27
          • 2011-10-04
          相关资源
          最近更新 更多