在uikeyboard顶部添加工具栏

use*_*878 17 cocoa-touch objective-c uitoolbar ios

我有一个工具栏,我想把它放在键盘的顶部.

在keyboardwillshow通知中,我试图将工具栏添加到键盘但没有运气,我无法添加

请告诉我

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];
        }
    }
Run Code Online (Sandbox Code Playgroud)

Vik*_*ica 44

我想,你想要一个inputAccessoryView.

基本上,您创建一个视图并将其设置为文本字段或文本视图的输入附件视图.

[textField setInputAccessoryView:inputAccessoryView];
Run Code Online (Sandbox Code Playgroud)


小智 14

这是代码以防其他人需要它.发现堆栈溢出

- (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];
}
Run Code Online (Sandbox Code Playgroud)