Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding tool bar on top of the uikeyboard

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];
        }
    }
like image 549
user198725878 Avatar asked Jan 25 '12 05:01

user198725878


People also ask

How do you add a bar to your keyboard?

It is located right above the Enter key. Another way to type the vertical bar character is to turn on the numeric keypad, hold ALT , then press 1, 2, and 4.

What is Inputaccessoryview in Swift?

A component which enables customization of the keyboard input accessory view on iOS. The input accessory view is displayed above the keyboard whenever a TextInput has focus. This component can be used to create custom toolbars.


2 Answers

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];
like image 165
vikingosegundo Avatar answered Oct 20 '22 13:10

vikingosegundo


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];
}
like image 42
mhrrt Avatar answered Oct 20 '22 12:10

mhrrt