【问题标题】:Custom background image to UITableView, but have tableHeaderView ignore it and remain transparent?UITableView 的自定义背景图像,但 tableHeaderView 忽略它并保持透明?
【发布时间】:2012-10-31 21:44:15
【问题描述】:

卡在某事上,我不确定这是否可能。有没有办法将 UITableView 的背景设置为自定义图像,但不要让该背景应用于 tableHeaderView。我的桌子上有一个需要保持透明的标题,因为我有一个视差类型效果(如路径 2 应用程序),它使用透明表格标题和表格视图顶部 1/3 后面的图像实现...但我需要在桌子的其余部分后面获取自定义图像。

我可以成功接近我正在寻找的填充在每个单元格后面的背景样式:

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

但是,这不是我要找的,因为我想要屏幕上整个 tableview 后面的径向渐变背景视图,减去透明标题......而不仅仅是每个单元格的相同图像。此外,这种方法确实影响了我的 tableview 的滚动性能,为每个单元格加载一个新的 BG 图像。

我知道你可以使用:

  UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];
  [tempImageView setFrame:self.tableView.frame];

  self.tableView.backgroundView = tempImageView;

为 tableview 设置 BG 图像,它真的很接近我想要做的,但我需要那个标题透明。有什么办法可以使用它,而且还告诉tableHeaderView忽略它并保持透明? 谢谢大家,万圣节快乐!

【问题讨论】:

  • 算了,默认的 UITableViewController 是最糟糕的,它不会做你想让它做的事情。创建一个普通的 UIViewController,把你的东西放在你想要的地方,然后自己实现协议,这是唯一的方法。

标签: iphone ios parallax uitableview


【解决方案1】:

是的,你可以。我为分组UITableView 实现了视差效果的解决方案。您可以使用相同的方法,除了可以使用您的图像而不是黑色背景(下面的示例)。本质上,您在 tableview 后面有两个视图(很清晰,标题视图背景清晰以及表格视图背景本身)。您基于滚动移动这两个视图 (UIScrollViewDelegate)。您的表格视图背景图像将与表格以一对一的方式“滚动”,而视差图像当然将以不同的速度“滚动”。在下面的示例中,我认为我的“_secondParaView”将是您的表格背景图片。

首先,在 viewDidLoad 中,创建一个视图来部分隐藏您的图像以获得视差效果,它应该与您希望 tableview 的背景颜色相同,在我的例子中是 blackColor。我根据图像的大小将视图放置在固定偏移处,您希望此视图的顶部与“第 0 节”标题视图的末端顶部对齐。然后它会在 tableview 滚动时“滚动”。将此视图插入到 tableview 下方。

_secondParaView = [[UIView alloc] initWithFrame: CGRectMake(0.0, kTableViewOffset, self.view.frame.size.width, 200.0)];
_secondParaView.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 1.0];
[self.view insertSubview: _secondParaView belowSubview: _tableView];

_headerImageYOffset = -40.0;
_headerImageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"SpaceRedPlanet640x480.png"]];
CGRect headerImageFrame = _headerImageView.frame;
headerImageFrame.origin.y = _headerImageYOffset;
_headerImageView.frame = headerImageFrame;
[self.view insertSubview: _headerImageView belowSubview: _secondParaView];
_tableView.backgroundColor = [UIColor clearColor];

然后实现header view/header view size的两个分组tableview方法:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        UIView * tableHeaderView = [[UIView alloc] initWithFrame: CGRectMake(0.0, 0.0, self.view.frame.size.width, kTableViewOffset)];
        tableHeaderView.backgroundColor = [UIColor clearColor];     
        return tableHeaderView;
    } else
        return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return kTableViewOffset;
    } else 
        return 2;
}

就像在“正常”的 tableview 视差实现中一样,让你的 VC 成为 UIScrollViewDelegate 并像这样实现这个 scrollViewDidScroll:

#pragma mark -
#pragma mark UIScrollViewDelegate methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat scrollOffset = scrollView.contentOffset.y;
    CGRect headerImageFrame = _headerImageView.frame;
    CGRect underParaFrame = _secondParaView.frame;

    if (scrollOffset < 0) {
        // Adjust top image proportionally
        headerImageFrame.origin.y = _headerImageYOffset - ((scrollOffset / 3));
    } else {
        // We're scrolling up, return to normal behavior
        headerImageFrame.origin.y = _headerImageYOffset - scrollOffset;
    }
    underParaFrame.origin.y = kTableViewOffset - scrollOffset;
    _headerImageView.frame = headerImageFrame;
    _secondParaView.frame = underParaFrame;
}

希望这会有所帮助,或者至少可以帮助某人为分组的 tableview 实现视差效果。我找不到解决办法。

【讨论】:

  • 嘿,谢谢你的回复,我直到现在才看到它。我会尽快检查一下。再次感谢您提供的详细信息。
【解决方案2】:

您是否有理由不能让tableHeaderView 成为您希望具有视差类型效果的图像的容器?

创建一个UIView 并将其作为UITableViewtableHeaderView,然后将您的UIImageView(或其他)添加到该tableHeaderView。使用UIScrollView 的委托方法,您将能够重新定位此UIView,但您可以在其父视图中响应用户滚动。

查看这个开源项目以获取路径灵感的视差UITableView:RBParallaxTableViewController

【讨论】:

  • 上周我看到了 RBParalax 示例,老实说,我无法真正弄清楚其中发生了什么。我什至在代码中的任何地方都没有看到 tableHeaderView 的使用(如果这很愚蠢,请不要笑……哈哈,我还在学习)。我不确定我是否可以通过在标题本身中嵌入 UIView 来实现我想要的效果......但如果你认为它是可行的,我真的很感激一些示例代码或现有的文章/博客来指出我正确的方向,甚至是对该 RBParallax 项目中正在发生的事情的快速分解。谢谢
猜你喜欢
  • 2011-08-21
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多