如何在键盘上方实现一个按钮? (例如:“笔记”应用程序,或者像这样 这个)。
MKaro的解决方案可行!我附上了Swift 3版本,以便更快地复制粘贴。
Swift 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
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;