如何在键盘上方实现按钮?

时间:2016-02-07 14:55:54

标签: ios swift

如何在键盘上方实现按钮? (例如:“Notes”应用程序,或 like this

2 个答案:

答案 0 :(得分:8)

MKaro的解决方案有效!我将附加Swift 3版本以便更快地复制粘贴。

斯威夫特3:

let keyboardToolbar = UIToolbar()
keyboardToolbar.sizeToFit()
keyboardToolbar.isTranslucent = false
keyboardToolbar.barTintColor = UIColor.white

let addButton = UIBarButtonItem(
    barButtonSystemItem: .done, 
    target: self, 
    action: #selector(someFunction)
)
addButton.tintColor = UIColor.black
keyboardToolbar.items = [addButton]
textView.inputAccessoryView = keyboardToolbar

答案 1 :(得分:1)

简单,您只需要使用UIToolBar和UIBarButtonItem。

这是一个简单的例子,它使"特殊键盘"对于特定的TextField:

此代码创建工具栏:

UIToolbar *keyboardToolbar = [[UIToolbar alloc] init];
[keyboardToolbar sizeToFit];
keyboardToolbar.translucent=NO; //if you want it.
keyboardToolbar.barTintColor = Some Color; //the color of the toolbar

此代码创建您想要的按钮

UIBarButtonItem *doneBarButton = [[UIBarButtonItem alloc]
                                  initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                  target:self.view action:@selector(set your own function here:)];
[doneBarButton setImage:CHOOSE YOUR IMAGE];
keyboardToolbar.items = @[doneBarButton];//you can add couple of buttons if you want to.
yourtextfield.inputAccessoryView = keyboardToolbar;