ios - 如何在多个视图控制器中使用自定义工具栏而不重复代码和复制元素(swift)?
问题描述
如何在多个视图控制器中使用自定义工具栏而不重复代码和复制元素(swift)?
尝试使用工具栏创建 XIB
“我在问我是否已经自定义了工具栏,我需要在多视图控制器中使用它,而无需重复我已经做过的事情”
解决方案
从故事板
- 转到故事板
- 选择你的初始 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
推荐阅读
- bash - 循环遍历 shell 脚本参数并将引用的参数传递给函数
- .net - 由于 dotnet 版本,VSTS 建立在干净的基础上
- javascript - Angular 4 数据表参数
- git - 使用接收后挂钩删除不相关的文件
- python-3.x - 无法从 Windows 10 pro 的 Anaconda 导入 sklearn 和 statsmodels
- oracle - 分析工作区管理器无法启动
- google-cloud-firestore - 来自不同集合的条件的 Firestore 规则
- wamp - 找不到 CORE WAMP 下载,一直只给我插件
- php - Wordpress 前端发布表单 - 图片未上传
- r - 提取 Survfit 线的点的坐标