修复了顶部 UITableView 中的 UIImage

Fixed UIImage in UITableView at top

当我在带有原型单元格的 UITableView 顶部添加 UIImage 时,图像不会保持固定。因此,当我滚动顶部的 UITableView 时,它也会随之滚动。

是否有任何想法将 UIImage 视图添加为固定(不可移动)

谢谢!

你可以做的是制作一个 UIView subclass 并使命中测试失败,从而将所有触摸事件传递给下面的 table 视图,这将允许你使用在图像视图内滚动手势而不移动。

在您的实施中:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

    UIView *view = [super hitTest:point withEvent:event];

    if (view == self) {

        view = nil;

    }

    return view;
}

UIImageView 放在 table 视图上,即将其添加到 table 视图的父视图(确保启用了用户交互并在 class故事板给你的子class.

在 UIImageView 中设置图片或在 UITableView header 中添加为 UIView 背景,使用以下方法。抱歉,下面的代码适用于 swift,您也可以找到适用于 Objective C 的代码。

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

}

您需要遵循以下步骤:

  1. 将您的基本视图保持为常规 UIView。
  2. 在 say (0,10) 添加图像作为子视图
  3. 在 (0, 10+margin) 位置添加一个 UITableView。

UITableView 是 UIScrollView 的子类。所以我们可以使用 scrollViewDidScroll 方法来执行这个任务。

将 imageView 固定在顶部很简单。但是您需要在 scrollViewDidScroll 方法中获取您的 imageView 的引用。

func scrollViewDidScroll(scrollView: UIScrollView) {

    if scrollView==self.tableView {

        var frame = yourImageView.frame

        if scrollView.contentOffset.y > 0 {

            frame.origin.y = scrollView.contentOffset.y
            yourImageView.frame = frame

        } else if scrollView.contentOffset.y == 0 {

            frame.origin.y = 0
            yourImageView.frame = frame
        }
    }
}