如何在 UIViewController 的 tableView 顶部添加自定义视图并能够一起滚动

Posted

技术标签:

【中文标题】如何在 UIViewController 的 tableView 顶部添加自定义视图并能够一起滚动【英文标题】:How to add a custom view to top of tableView in UIViewController and be able to scroll together 【发布时间】:2015-12-03 14:13:04 【问题描述】:

我正在尝试在我的UIViewController 中的UITableView 顶部放置一个大的UIImageView。我读到我不应该将UITableView 放在 UIScrollView 中,所以我试图找出最好的方法。

现在,在我的UIViewController 中,我有一个巨大的UIImageView(覆盖了屏幕的一半),下面是我的UITableView。 问题是滚动只适用于UITableViewCell,但我不能向上滚动,这样图像就到了顶部并且不可见......

感谢您的帮助!

【问题讨论】:

可以使用一个tableview,可以把image view放在table view第一个cell!!! @Mr.T 知道了。非常简单。 UITableView 中不需要滚动视图的原因,因为 scrollView 是 UITableView 的子类。 【参考方案1】:

使 tableview 成为视图控制器视图的高度,然后在 viewDidLoad 中说:

self.imageView.removeFromSuperView()
self.tableView.tableHeaderView = self.imageView

如果您使用自动布局,则应将第二行移至 viewsDidLayoutSubviews

【讨论】:

以上是关于如何在 UIViewController 的 tableView 顶部添加自定义视图并能够一起滚动的主要内容,如果未能解决你的问题,请参考以下文章

如何在通用 UIViewController 之后设置自定义 UIViewController?

iOS:如何在弹出其子 UIViewController 后“刷新” UIViewController?

iPad 应用程序,如何在 uiviewcontroller 之上显示 uiviewcontroller

如何在 UITabBarViewController 中的 UINavigationController 中深度链接到从 UIViewController 访问的 UIViewController?

如何成为游戏技术美术(TA)

如何在不导入第一个 UIViewController 类的情况下从 UINavigationController 中的另一个 UIViewController 手动释放 UIViewControlle