Swift:inputAccessoryView按钮未显示

时间:2015-05-13 15:52:00

标签: ios xcode swift uibarbuttonitem inputaccessoryview

以下是我的textView上添加inputAccessoryView(带有Done按钮)的代码:

let keyboardButtonView = UIToolbar()
keyboardButtonView.sizeToFit()
let doneButton = UIBarButtonItem(image: nil, style: .Done, target: self, action: "closeMessageViewKeyboard")
doneButton.possibleTitles = ["Done"]
var toolbarButtons = NSMutableArray()
toolbarButtons.addObject(doneButton)
keyboardButtonView.items = toolbarButtons as [AnyObject]
messageView.inputAccessoryView = keyboardButtonView

完成按钮永远不会出现。我得到的只是一个白色的配件吧。我在这里错过了什么吗?

2 个答案:

答案 0 :(得分:2)

对我来说,我使用UINavigationBar创建配件视图,如下所示:

let navBar = UINavigationBar(frame: CGRectMake(0, 0, viewWidth, 44))
navBar.barStyle = UIBarStyle.BlackTranslucent;
navBar.backgroundColor = UIColor.blackColor();
navBar.alpha = 0.9;
//replace viewWidth with view controller width
let navItem = UINavigationItem()
let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "closeMessageViewKeyboard")
navItem.rightBarButtonItem = doneButton

navBar.pushNavigationItem(navItem, animated: false)

messageView.inputAccessoryView = navBar

答案 1 :(得分:0)

1)加入自我? self.messageView.inputAccessoryView = keyboardButtonView

2)您应该可以删除此行: doneButton.possibleTitles = ["完成"] 并添加(标题:"完成",...)到它上面的一行。

3)我有类似的代码,它可能并不重要,但是,您可以添加: keyboardButtonView.barStyle = UIBarStyle.Default