UILabel不会在UIToolbar中显示

时间:2017-07-24 03:32:36

标签: ios swift

我正在尝试在UIToolbar上添加一个Done按钮和一个Label,但只显示Done按钮。这是我的代码:

func addDoneBtnAndCountLabelToKeyboard() {
    let keyboardToolbar = UIToolbar()
    keyboardToolbar.sizeToFit()
    keyboardToolbar.isTranslucent = false
    keyboardToolbar.barTintColor = UIColor.lightGray
    //creating flexibleSpace
    let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)

    let addButton = UIBarButtonItem(
        barButtonSystemItem: .done,
        target: self,
        action: #selector(CreateClubTVC.hideKeyboard)
    )

    let label = UILabel()
    label.text = "1"
    label.backgroundColor = UIColor.red
    let addLabel = UIBarButtonItem.init(customView: label)

    addButton.tintColor = UIColor.black

    keyboardToolbar.items = [addLabel, flexibleSpace,addButton]

    clubNameTextField.inputAccessoryView = keyboardToolbar
    clubExplanationTV.inputAccessoryView = keyboardToolbar
}

我基本上希望label.text显示clubNameTextField或clubExplanationTV中的字符数。

1 个答案:

答案 0 :(得分:1)

您需要完成标签的设置。您已经创建了一个没有大小的标签。

如果您希望标签恰好适合您指定的文字,请在设置标签sizeToFit()后调用标签上的text