4

How to implement a button above the keyboard? (For example: "Notes" app, or like this

user4478196
  • 157
  • 2
  • 8
  • Possible duplicate of [How to make a UITextField move up when keyboard is present](http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present) – xoudini Feb 07 '16 at 14:58
  • you want a button to stick to the keyboard top edge? – DeyaEldeen Feb 07 '16 at 15:02
  • @Zazu yes, if it is also implemented in Apple's Notes – user4478196 Feb 07 '16 at 16:09
  • Possible duplicate of [Next/Done button using Swift with textFieldShouldReturn](http://stackoverflow.com/questions/29925851/next-done-button-using-swift-with-textfieldshouldreturn) – DeyaEldeen Feb 07 '16 at 16:33

2 Answers2

9

MKaro's solution works! I'll attach the Swift 3 version for faster copy-paste.

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
shannoga
  • 19,000
  • 20
  • 99
  • 163
1

Its simple, all you need is to use UIToolBar and UIBarButtonItem.

Here is a simple example, that make "Special Keyboard" for specific TextField:

This code create the toolbar:

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

this code create the button that you want

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;
MKaro
  • 156
  • 9
  • 1
    It appears he uses swift. – DeyaEldeen Feb 07 '16 at 16:16
  • 1
    I know, but this is the way it should be done. it was more simple for me to write it in obj-c. the more important thing is to get the idea and i hope that it will help him to understand. – MKaro Feb 07 '16 at 16:32