首页 > 解决方案 > 如何在多个视图控制器中使用自定义工具栏而不重复代码和复制元素(swift)?

问题描述

如何在多个视图控制器中使用自定义工具栏而不重复代码和复制元素(swift)?

尝试使用工具栏创建 XIB

“我在问我是否已经自定义了工具栏,我需要在多视图控制器中使用它,而无需重复我已经做过的事情”

标签: iosswiftiphonetoolbarviewcontroller

解决方案


从故事板

  • 转到故事板
  • 选择你的初始 ViewController
  • 在顶部单击编辑器>嵌入>导航控制器
  • 选择导航控制器,转到 Attributes Inspector>Bottom Bar 并选择所需的工具栏。

现在每次创建新的 ViewController 并执行 segue 时,工具栏都会自动出现。

以编程方式

在你的 rootViewController 的 viewDidLoad()

let toolBar = UINib(nibName: "ToolBar", bundle: nil).instantiateWithOwner(nil, 
options: nil).first as! YourToolBarClass
self.navigationController!.view.addSubview(toolBar)
self.navigationController!.toolbarItems = toolBar.items

对所有视图控制器:

self.toolbarItems = self.navigationController!.toolbarItems

推荐阅读