如何更新工具栏按钮
How do I update the Toolbar Buttons
我正在使用 this phenomenal framework and had difficulties updating buttons on the toolbar. I followed the sample code of the NavigationDrawerController。所以最初,工具栏在左侧填充了一个 menuButton,在右侧填充了另外两个按钮:
// From AppToolbarController.swift
fileprivate func prepareToolbar() {
toolbar.leftViews = [menuButton]
toolbar.rightViews = [switchControl, moreButton]
}
现在,当我想从另一个 ViewController 更改工具栏中的按钮时,我(尽管我很天真)执行以下操作:
// From RootViewController.swift
fileprivate func prepareToolbar() {
guard let tc = toolbarController else {
return
}
tc.toolbar.rightViews = [someOtherButton]
}
但是,这没有任何效果,按钮保持不变。此方法仅在之前未设置 toolbar.rightViews 时对我有效。
更新工具栏按钮的正确方法是什么?
我认为问题可能出在您从 viewDidLoad
函数调用更新 Toolbar
(prepareToolbar
) 函数。问题是 RootViewController
实际上没有连接到 toolbarController
。尝试将 prepareToolbar
函数移动到视图控制器的 viewWillAppear
函数。如果这没有帮助,您可以显示代码设置吗?祝一切顺利!
代码示例:
class RootViewController: UIViewController {
fileprivate var remindersButton: IconButton!
open override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = Color.white
}
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
prepareRemindersButton()
prepareToolbar()
}
}
extension RootViewController {
fileprivate func prepareRemindersButton() {
remindersButton = IconButton(image: Icon.cm.bell, tintColor: .white)
remindersButton.pulseColor = .white
}
fileprivate func prepareToolbar() {
guard let toolbar = toolbarController?.toolbar else {
return
}
toolbar.title = "Material"
toolbar.titleLabel.textColor = .white
toolbar.titleLabel.textAlignment = .left
toolbar.detail = "Build Beautiful Software"
toolbar.detailLabel.textColor = .white
toolbar.detailLabel.textAlignment = .left
toolbar.rightViews = [remindersButton]
}
}
我正在使用 this phenomenal framework and had difficulties updating buttons on the toolbar. I followed the sample code of the NavigationDrawerController。所以最初,工具栏在左侧填充了一个 menuButton,在右侧填充了另外两个按钮:
// From AppToolbarController.swift
fileprivate func prepareToolbar() {
toolbar.leftViews = [menuButton]
toolbar.rightViews = [switchControl, moreButton]
}
现在,当我想从另一个 ViewController 更改工具栏中的按钮时,我(尽管我很天真)执行以下操作:
// From RootViewController.swift
fileprivate func prepareToolbar() {
guard let tc = toolbarController else {
return
}
tc.toolbar.rightViews = [someOtherButton]
}
但是,这没有任何效果,按钮保持不变。此方法仅在之前未设置 toolbar.rightViews 时对我有效。
更新工具栏按钮的正确方法是什么?
我认为问题可能出在您从 viewDidLoad
函数调用更新 Toolbar
(prepareToolbar
) 函数。问题是 RootViewController
实际上没有连接到 toolbarController
。尝试将 prepareToolbar
函数移动到视图控制器的 viewWillAppear
函数。如果这没有帮助,您可以显示代码设置吗?祝一切顺利!
代码示例:
class RootViewController: UIViewController {
fileprivate var remindersButton: IconButton!
open override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = Color.white
}
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
prepareRemindersButton()
prepareToolbar()
}
}
extension RootViewController {
fileprivate func prepareRemindersButton() {
remindersButton = IconButton(image: Icon.cm.bell, tintColor: .white)
remindersButton.pulseColor = .white
}
fileprivate func prepareToolbar() {
guard let toolbar = toolbarController?.toolbar else {
return
}
toolbar.title = "Material"
toolbar.titleLabel.textColor = .white
toolbar.titleLabel.textAlignment = .left
toolbar.detail = "Build Beautiful Software"
toolbar.detailLabel.textColor = .white
toolbar.detailLabel.textAlignment = .left
toolbar.rightViews = [remindersButton]
}
}