使用 UINavigationController 时在 UITableView 顶部插入图像?

Posted

技术标签:

【中文标题】使用 UINavigationController 时在 UITableView 顶部插入图像?【英文标题】:Insert image at top of UITableView when using UINavigationController? 【发布时间】:2011-03-09 07:46:09 【问题描述】:

我的 MainWindow.xib 中有一个 UINavigationController 和 UITableView。我正在尝试在视图顶部插入一个不可触摸/不可移动的图像。 (我不想简单地改变 UINavigationBar 背景,根据我的经验,UINavigationBar 的高度不能随意增加。)

由于这是一个根视图,它的行为由我的 RootViewController 控制。当视图加载时,我使用self.navigationController.navigationBarHidden = YES;隐藏导航栏

我尝试将 UIView 添加到 UITableView 的顶部,但是当用户滚动表格时,顶部图像会移动。如何在 UITableView 顶部放置静止图像而不像单元格一样移动并且不使用 UINavigationBar 背景图像?

天真的可能性:

将视图包含在另一个视图中,其顶部包含图像? 在屏幕顶部叠加图像遮罩?

(HIG 违规,有人吗?)

【问题讨论】:

【参考方案1】:

如果您的视图控制器是UITableViewController,您不能轻易添加另一个视图,因为tableViewController 只管理一个视图,即UITableView。我建议使用普通的UIViewController,而不是为您的顶视图添加一个UIView,为您的内容添加一个UITableView,作为主viewController.view 的子视图。然后,在您的UIViewController 中实现UITableViewDelegateUITableViewDataSource 协议。

【讨论】:

我最初走的是类似的道路,但我认为导航控制器将帮助我管理我的应用程序的层次结构,并允许方便的后退箭头(UIToolBar 中不允许这样做)。我不相信导航控制器有多大帮助。 您可以在UINavigationController 中嵌入UIViewControllerUITabBarController 中的两个子视图。这样你就可以得到世界上最好的。通过覆盖-drawRect: 可以自定义导航栏的背景。但是,如果您还打算自定义后退按钮的外观,请准备好忍受痛苦。【参考方案2】:

您可以创建另一个UIViewController,其中包含UITableViewController 和顶部的静态图像。这样,您甚至可以调整表格视图的大小,使静态图像显示在表格上方而不是表格上方。

UIViewController *middleViewController = [[UIViewController alloc] init];
[[middleViewController view] addSubview:tableView];
[[middleViewController view] addSubview:staticImageView];
...
[navigationController initWithRootViewController:middleViewController];

自从我上一个可可应用程序以来已经很长时间了,所以我不能保证它会起作用。

【讨论】:

我想你的意思是 [[middleViewController view] addSubview:tableView];

以上是关于使用 UINavigationController 时在 UITableView 顶部插入图像?的主要内容,如果未能解决你的问题,请参考以下文章

UINavigationcontroller 中未使用的视图会发生啥?

使用 push segue 时 UINavigationController 是不是强制

UINavigationController - 使用 UIBlurEffect 清除背景

一起使用 UITabBarController 和 UINavigationController

iPhone - UINavigationController - 使用 CATransaction 自定义动画

如何在 SwiftUI 中使用 UINavigationController