ios - 使用 IQKeyBoardManager 时键盘 titleBarButton 的属性文本问题
问题描述
我正在将 IQKeyboardManager 与 Swift 一起使用。我在日期选择器上添加了一个取消按钮。一切正常,但我遇到了取消按钮的属性文本没有生效的问题。我究竟做错了什么?这是一个代码片段
cell.field.attributedPlaceholder = NSAttributedString(string: "Cancel",
attributes: [.foregroundColor: UIColor.black,
.font: UIFont.boldSystemFont(ofSize: 12)])
cell.field.keyboardToolbar.titleBarButton.setTarget(self,
action:#selector(datePickerWasCanceled(sender:)))
这是当前结果的示例屏幕截图——我原以为取消应该是黑色和粗体。
解决方案
使用 UIBarButtonItem 而不是 attributesPlaceholder,两边都有一个灵活的空间,这样您就可以将它放置在工具栏的中间。
var items: [UIBarButtonItem] = []
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancel: UIBarButtonItem = UIBarButtonItem(title: "Cancel", style: .done, target: self, action: #selector(self.datePickerWasCanceled))
cancel.setTitleTextAttributes([
NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 17.0),
NSAttributedStringKey.foregroundColor: UIColor.blue], for: .normal)
items.append(flexSpace)
items.append(cancel)
items.append(flexSpace)
cell.field.keyboardToolbar.items = items
cell.field.keyboardToolbar.sizeToFit()
@objc func datePickerWasCanceled() {
cell.field.resignFirstResponder()
}
推荐阅读
- http-method - AppScan 问题:Liberty 服务器欢迎页面上的“不安全的“OPTIONS”HTTP 方法已启用”
- javascript - 点击时显示表单
- python - 使用 Python 和 Keras 创建两层简单 RNN 模型
- python - 如何解决此 KeyError?
- c - c中的“无效声明”是什么意思?
- python - 如何使默认的 matplotlib 光标“+”跨越整个 x 轴?
- node.js - 如何有效地将所有子数组与相关 id 组合并删除所有重复对象
- ionic-framework - 如何使用 IONIC 4 在后台模式下流式传输音频
- python - Python:无法导入已安装的模块 pdpbox
- c# - 如何将带有本地安装的 MongoDB 的 Asp.net 核心 Web api 部署到 Microsoft Azure?