ios - 按下后如何更改 UIBarButtonItem 的 titleTextAttributes?
问题描述
我有一个带有几个 UIBarButtons 的导航栏,我最初以 0.5 的 alpha 显示,并希望在按下后以 1 的 alpha 显示。我将这些属性存储在 normalAttributes 和 highlightAttributes 中。
我按以下方式设置按钮:
colorButton = UIBarButtonItem(title: "Colors", style: .plain, target: self, action: #selector(showColors))
colorButton.setTitleTextAttributes(normalAttributes, for: .normal)
colorButton.setTitleTextAttributes(highlightedAttributes, for: .selected)
这导致按钮短暂切换到突出显示的属性,这是预期的。但是,在按钮的操作 showColors 中,我然后执行以下操作:
textButton.setTitleTextAttributes(highlightedAttributes, for:[])
我也尝试过使用 .normal 而不是 []。这两种方法似乎都对按钮没有任何影响。对此的任何帮助将不胜感激。
解决方案
我能想到的最简单的方法是使用自定义视图。意思是,创建一个单独UIButton
的并在您的类中引用它(例如,let yourButton = UIButton()
)。初始化您的栏按钮项目,UIBarButtonItem(customView: yourButton)
然后根据需要使用它。
请注意,您需要将action
/target
对添加到 上yourButton
,而不是栏按钮项上。然后在您的action
处理程序showColors
中,根据您的情况,您可以更改yourButton
.
您可以alpha
直接切换,或者如果它只是我们正在谈论的文本,您可以这样做:
yourButton.setTitleColor(yourColor, for: .normal)
和
yourButton.setTitleColor(yourColorHalfAlpha, for: .selected)
。
然后在你的showColors
,只需调用yourButton.isSelected = !yourButton.isSelected
来切换外观。
推荐阅读
- javascript - 如何将 Pixlee Social Feed 添加到 React 应用程序?
- r - 使用 dplyr 计算 R 中的分组序列
- ruby-on-rails - 是否可以为 Algolia 记录动态创建属性?
- html - 网站中的 XHTML-Doctype 仍然可以使用吗?
- sql - Datediff SQL 日期转换
- python - 使用 Pandas 计算分组时的案例
- node.js - 在 Linux 服务器上运行 .exe 文件
- r - 选中一些复选框后,进行一些测试并显示输出:R shiny
- spring - 为什么 Spring 在返回 CompletableFuture 时会提出 DeferredResult 达到相同的结果
- php - Zend Framework 1.12 - 如何在 pdf 中渲染 Zend_form