使用IQKeyBoardManager时键盘titleBarButton的属性文本有问题

时间:2018-12-10 20:03:11

标签: ios swift iqkeyboardmanager

我正在使用带有Swift的IQKeyboardManager。 我在日期选择器上添加了一个取消按钮。 一切正常,但是我无法确定取消按钮的属性文本是否生效。我究竟做错了什么? 这是一个代码片段

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:)))

这是当前结果的示例屏幕截图-我本以为取消应以黑色和粗体显示。

keyboard cancel button does not have specified attributes

2 个答案:

答案 0 :(得分:1)

使用UIBarButtonItem而不是attributedPlaceholder左右两边,以便将其放置在工具栏的中间。

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()
}

答案 1 :(得分:0)

cell.field.attributedPlaceholder = NSAttributedString(string: "Cancel") cell.field.keyboardToolbar.titleBarButton.titleFont = UIFont.boldSystemFont(ofSize: 17.0) cell.field.keyboardToolbar.titleBarButton.selectableTitleColor = UIColor.black

cell.field.keyboardToolbar.titleBarButton.setTarget(self, action:#selector(datePickerDidCancel(sender:))