ios - 如何用经典的 UIButton 替换 UIBarButtonItem
问题描述
let rightBarBtn = UIBarButtonItem(title: "Light", style: .plain, target: self, action: #selector(rightBarBtnAction))
self.navigationItem.rightBarButtonItem = rightBarBtn
}
@objc func rightBarBtnAction(sender: UIBarButtonItem) {
if theme == .dark {
sender.title = "Dark"
theme = .light
Style.themeLight()
} else {
sender.title = "Light"
theme = .dark
Style.themeDark()
}
self.view.backgroundColor = Style.bgColor
calenderView.changeTheme()
}
这是一段代码,负责在我的日历中的两个主题之间切换,我想将 UIBarButton 切换到 UIButton 以更改其在屏幕上的位置。
到目前为止,这是我想出的:
let themeSwitch = UIButton()
themeSwitch.setTitle("Light", for: .normal)
themeSwitch.backgroundColor = .darkGray
themeSwitch.addTarget(self, action: #selector(themeSwitchAction), for: .touchUpInside)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
calenderView.myCollectionView.collectionViewLayout.invalidateLayout()
}
@objc func themeSwitchAction(sender: UIButton) {
if theme == .dark {
sender.currentTitle = "Dark"
theme = .light
Style.themeLight()
} else {
sender.currentTitle = "light"
theme = .dark
Style.themeDark()
}
self.view.backgroundColor = Style.bgColor
calenderView.changeTheme()
}
正如您可能知道的那样,这不起作用。有人可以告诉我正确执行此操作的方法吗?
解决方案
推荐阅读
- java - 使用 gradle 将清单文件添加到 jar
- javascript - 在反应中,在 Render() 之前执行 API 调用和重定向的最佳方法是什么?
- python - 如何使用 python Telegram Bot API 将文件上传到谷歌驱动器
- c - 用 C 写位图
- android - 错误:无法解决:同步 Android 项目时的 firebase-auth-license
- python - 如何将纬度归一化到-90到90的范围内?
- python-3.x - 应用比较运算符后如何输出到另一列
- python - 我怎么知道我的电报机器人用户按下了按钮?
- angular - 需要帮助传递用户输入数据以动态更改表单
- angular - 如何使用 HttpRepsonse 从 post 方法获得回报