How can I add a toolbar above the keyboard?

后端 未结 9 1326
孤街浪徒
孤街浪徒 2020-11-27 10:35

I have created a UIToolBar programmatically and added a UITextField on it. Now, I need that toolbar to be above the keyboard when I click in anothe

相关标签:
9条回答
  • 2020-11-27 11:20
    textField.inputAccessoryView=[weakSelf addToolBar];
    [textField setKeyboardType:UIKeyboardTypeNumberPad];
    

    and add a method

    -(UIToolbar *)addToolBar
    {
    
        UIBarButtonItem *done=[[UIBarButtonItem alloc]initWithTitle:@"DONE" style:UIBarButtonItemStyleDone target:self action:@selector(done:)];
        UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
        NSArray* toolBarItems=[[NSArray alloc]initWithObjects:done, nil];
        [toolBar setItems:toolBarItems];
        return toolBar;
    }
    
    0 讨论(0)
  • 2020-11-27 11:23

    For swift (1.2):

    let numberToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))
    numberToolbar.barStyle = UIBarStyle.Default
    
    numberToolbar.items = [
        UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardCancelButtonTapped:"),
        UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil),
        UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardDoneButtonTapped:")]
    
    numberToolbar.sizeToFit()
    yourTextView.inputAccessoryView = numberToolbar
    
    0 讨论(0)
  • 2020-11-27 11:30

    You Can Use this code it work for me.

    -(void)viewdidload
    {
     UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
     [keyboardDoneButtonView sizeToFit]; 
     UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                            style:UIBarButtonItemStyleBordered target:self
                                                                      action:@selector(doneClicked:)];
      [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
      textField.inputAccessoryView = keyboardDoneButtonView;
     }
    -(void)doneClicked:(id)sender
    {
    NSLog(@"Done Clicked.");
    [self.view endEditing:YES];
    } 
    
    0 讨论(0)
提交回复
热议问题