17

I have a toolbar and i would like to place it on top of the keyboard.

In the keyboardwillshow notification, i tried to add toolbar to the keyboard but no luck, i cant add

Please let me know

UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i = 0; i < [tempWindow.subviews count]; i++)
    {
        //Get a reference of the current view 
        keyboard = [tempWindow.subviews objectAtIndex:i];

        //Check to see if the description of the view we have referenced is "UIKeyboard" if so then we found
        //the keyboard view that we were looking for
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
        {
            [keyboard addSubview:myToolbar];
        }
    }
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
user198725878
  • 6,266
  • 18
  • 77
  • 135

5 Answers5

44

I think, you want an inputAccessoryView.

Basically, you create a view and set it as a text field's or text view's input accessory view.

[textField setInputAccessoryView:inputAccessoryView];
Pang
  • 9,564
  • 146
  • 81
  • 122
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
15

Here is code in case someone else need it.Found on stack overflow

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    numberToolbar.barStyle = UIBarStyleBlackTranslucent;
    numberToolbar.items = [NSArray arrayWithObjects:
                         [[UIBarButtonItem alloc]initWithTitle:@"Clear" style:UIBarButtonItemStyleBordered target:self action:@selector(clearNumberPad)],
                         [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                         [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                     nil];
    [numberToolbar sizeToFit];
    numberTextField.inputAccessoryView = numberToolbar;
}



-(void)clearNumberPad{
    [numberTextField resignFirstResponder];
    numberTextField.text = @"";
}

-(void)doneWithNumberPad{
    NSString *numberFromTheKeyboard = numberTextField.text;
    [numberTextField resignFirstResponder];
}
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
mhrrt
  • 977
  • 9
  • 18
3

https://github.com/asefnoor/IQKeyboardManager

This is the best keyboard handler I have seen. Very excellent way to manage Text inputs.

Some of its features 1) ZERO LINE OF CODE

2) Works Automatically

3) No More UIScrollView

4) No More Subclasses

5) No More Manual Work

6) No More #imports

i.AsifNoor
  • 587
  • 5
  • 14
1

Simple solution for iOS 8 and 9.

Init your textField, searchBar etc.

customView - View that will stick at the top of the keyboard. DO not add it as a subview to hierarchy! Do not have to set any constraints or positions.

[searchBar setInputAccessoryView:self.customView];

- (BOOL)canBecomeFirstResponder{
    return true;
}

- (UIView *)inputAccessoryView {
    return self.customView;

}

If you need to let the customView at the bottom and not hide it after dismissing keyboard, add observer:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:self.view.window];

And method

- (void)keyboardDidHide:(NSNotification *)notification {
    [self becomeFirstResponder];
}
Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84
1

if you want to add tool bar on the keyboard, you are right place. You can use BSKeyboard easly. Look at the implementation below;

in .h file

#import "BSKeyboardControls.h"

add delegates

@interface ViewController : UIViewController <BSKeyboardControlsDelegate>

now jump the .m file , go to viewDidLoad. We will add textfield which i want to add tollbar

self.keyboardControls = [[BSKeyboardControls alloc] initWithFields:@[PINTextField]];
[self.keyboardControls addDelegate:self];

we should add activeField just like below,

- (void)textFieldDidBeginEditing:(UIView *)textField {
    [self.keyboardControls setActiveField:textField];
}

And last step is delegate metods implemantation,

 - (void)keyboardControlsDonePressed:(BSKeyboardControls *)keyboardControls {
    [super textFieldShouldReturn:self.keyboardControls.activeField];
}

That's it.

For more info and download the BSKeyboard files, you can move the below link BSKeyboard Githup Link

Emre Gürses
  • 1,992
  • 1
  • 23
  • 28