自定义选项卡栏上的“更多”菜单
我在我的应用程序上使用选项卡栏 (UITabBarController),我希望自定义单击“更多”按钮时出现的表格的外观。 我已经研究出如何通过
self.moreNavigationController.navigationBar.barStyle
在 UITabBarController 的子类中进行设置来更改更多屏幕上的导航栏的外观,并且我已设法通过修改来更改表格的背景颜色
self.moreNavigationController.topViewController.view.backgroundColor
,但我无法弄清楚如何更改表格中显示的单元格中的字体颜色。 我希望我能使用,
self.moreNavigationController.topViewController.view.visibleCells
但这似乎总是空的。 我尝试在 viewDidLoad、viewWillAppear 和 viewDidAppear 中执行此操作,但没有成功。 对象 self.moreNavigationController.topViewController 的类型为 UIMoreListController,它似乎没有记录,我在界面中看不到任何对我有帮助的明显内容。
有任何想法吗?
I am using a tab bar (UITabBarController) on my app and I wish to customize the appearance of the table that appears when you click the more button.
I have worked out how to change the appearance of the Navigation bar that is on the more screen by setting
self.moreNavigationController.navigationBar.barStyle
in a subclass of UITabBarController and I have managed to change the background colour of the table by modifying
self.moreNavigationController.topViewController.view.backgroundColor
, but I cannot work out how to change the font colour in the cells that appear on the table.
I was hoping I could use
self.moreNavigationController.topViewController.view.visibleCells
but this always seems to be empty. I've tried doing this in viewDidLoad, viewWillAppear and viewDidAppear with no success. The object self.moreNavigationController.topViewController is of type UIMoreListController, which seems to be undocumented and I can't see anything obvious in the interface that will help me.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根据 Stephan 的建议,替换 moreNavigationController 的数据源,这里是我实现的代码的快速概览。
我创建了一个名为 MoreTableViewDataSource 的新类,它实现了 UITableViewDataSource 协议。 更多页面实际用于构建表的控制器称为 UIMoreListControllerModern,它仅实现 UITableViewDataSource 协议所需的部分。 我的实现看起来像这样。
然后在我的 CustomTabBarController 类中,我重写 viewDidLoad 如下:
按照此处的要求,是头文件
和
Following on from Stephan's suggestion to replace the dataSource of the moreNavigationController, here is a quick over view of the code I implemented.
I created a new class called MoreTableViewDataSource which implements the UITableViewDataSource protocol. The controller which the more page actually uses to build the table is called the UIMoreListControllerModern, and this implements just the required parts of the UITableViewDataSource protocol. My implementation looks like this.
and then in my CustomTabBarController class I override viewDidLoad as follows:
As requested here are the header files
and
仅在显示 moreNavigationController 后才填充visibleCells。
并且单元格是在运行时创建的,因此即使您更改单元格的内容,它们在显示时也会被替换。
要尝试的一件事是替换 moreNavigationController tableView 的数据源,调用原始数据源的 cellForRowAtIndexPath 并在返回之前更改其内容。
使用下面的代码,在显示一次 moreNavigationController 并对其进行初始化后,您将看到当您返回到 moreNavigationController 时,单元格为红色,但立即返回到白色背景。
visibleCells is populated only after the moreNavigationController is displayed.
And the cells are created at runtime, so even if you change the content of the cells, they are replaced when they are displayed.
One thing to try would be to replace the datasource of the moreNavigationController tableView, call the cellForRowAtIndexPath of the original datasource and change its content before returning it.
Using the code below, after having displayed once the moreNavigationController to initialize it, you'll see that when you return to the moreNavigationController, the cells are red, but return immediately to white background.
感谢未知。
按照他的解决方案,我将把他的代码放在 Swift 中。
您唯一应该做的就是创建 MoreTableViewCell 类并创建它。 您不必使用故事板。 如果你想修改tableView,你可以在customizeMoreTableView方法中完成。
Thanks to Unknown.
Following his solution, I will put his code in Swift.
Only what you should do more is create MoreTableViewCell class and just it. You don't have to use Storyboard. If you want to modify tableView you can do it in customizeMoreTableView method.
我按照 Ian 的实现来自定义“更多”菜单,但在出现内存警告后保留自定义项时遇到问题。 didReceiveMemoryWarning 似乎破坏了 UITableView,当它重新生成时,它会恢复旧的数据源。 这是我的解决方案:
我将 CustomTabBarController 上的 viewDidLoad 替换为:
如您所见,我添加了一些属性来存储我需要的内容。 这些必须添加到标题中并进行合成。 我还在标头中将 CustomTabBarController 设置为 UINavigationControllerDelegate。 这是我添加的委托函数:
这样我可以确保始终使用我的自定义数据源,因为我在每次显示 UIMoreListController 之前都以这种方式设置它。
I followed Ian's implementation to customize the More menu, but I was having a problem retaining the customizations after a memory warning. didReceiveMemoryWarning seems to destroy the UITableView, and when it is regenerated it gets its old dataSource back. Here's my solution:
I replace viewDidLoad on the CustomTabBarController with this:
As you can see, I added a few properties for storing things I needed. Those have to be added to the header and synthesized. I also made CustomTabBarController a UINavigationControllerDelegate in the header. Here's the delegate function I added:
This way I make sure my custom data source is always used, because I set it that way just prior to showing the UIMoreListController, every time it's shown.
这在 iOS 13、Swift 5.1 中适用于我:
This works for me in iOS 13, Swift 5.1:
我想我找到了一个更简单的解决方案...只需将此扩展放在选项卡栏控制器的登陆页面上即可。 这个函数是在菜单创建后设置菜单tableView,并且单元格都在tableView.visibleCells中
I think I found an easier solution... Just drop this extension on your landing page of Tab Bar Controller. This function is setting the menu tableView after the menu is created and the cells are all in tableView.visibleCells