How to add done button on keyboard on top of keyboard in IOS?

TechChain picture TechChain · Dec 14, 2015 · Viewed 34k times · Source

I want to add the Done button on the top of keyboard on right side in iOS for a textView.Please tell me how can i do that?

enter image description here

I want to achieve something similar to the above keyboard

Answer

Cong Tran picture Cong Tran · Dec 14, 2015

Hope this help :)

UIToolbar* keyboardToolbar = [[UIToolbar alloc] init];
[keyboardToolbar sizeToFit];
UIBarButtonItem *flexBarButton = [[UIBarButtonItem alloc]
                                  initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                  target:nil action:nil];
UIBarButtonItem *doneBarButton = [[UIBarButtonItem alloc]
                                  initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                  target:self action:@selector(yourTextViewDoneButtonPressed)];
keyboardToolbar.items = @[flexBarButton, doneBarButton];
self.yourTextView.inputAccessoryView = keyboardToolbar;

and then add yourTextViewDoneButtonPressed method

-(void)yourTextViewDoneButtonPressed
{
    [self.yourTextView resignFirstResponder];
}