How to add a toolbar above the keyboard?

I created the UIToolBar programmatically and added a UITextField to it. Now, when I click in another text box, I need a toolbar above the keyboard.

 UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0,400, 320, 60)]; [self.view addSubview:toolBar]; UITextField *txtView=[[UITextField alloc]initWithFrame:CGRectMake(0, 400, 260, 30)]; txtView.backgroundColor =[UIColor grayColor]; txtView.placeholder=@"Address"; UIBarButtonItem *txtfieldItem=[[UIBarButtonItem alloc]initWithCustomView:txtView]; toolBar.items =[NSArray arrayWithObject:txtfieldItem]; 
+63
ios uitextfield swift uitoolbar
May 28 '14 at 7:05
source share
8 answers
 UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 50)]; numberToolbar.barStyle = UIBarStyleBlackTranslucent; numberToolbar.items = [NSArray arrayWithObjects: [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)], [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)], nil]; [numberToolbar sizeToFit]; phonenumberTextField.inputAccessoryView = numberToolbar; 

Dismiss Keyboard:

 [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil]; 



Swift 3:

 let numberToolbar = UIToolbar(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 50)) numberToolbar.barStyle = UIBarStyle.Default numberToolbar.items = [ UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelNumberPad"), UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil), UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneWithNumberPad")] numberToolbar.sizeToFit() phonenumberTextField.inputAccessoryView = numberToolbar 

Swift 4.2:

 let numberToolbar = UIToolbar(frame:CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50)) numberToolbar.barStyle = .default numberToolbar.items = [ UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelNumberPad)), UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneWithNumberPad))] numberToolbar.sizeToFit() phonenumberTextField.inputAccessoryView = numberToolbar ... @objc func cancelNumberPad() { //Cancel with number pad } @objc func doneWithNumberPad() { //Done with number pad } 
+115
May 28 '14 at 7:09
source share

You no longer need to do this in code.

  • Just drag the UIView to the top panel of the current scene and adjust it as you want.

enter image description here

  1. In the code, simply enter an IBOutlet for both: toolbarView and textView and make the connections.

     @IBOutlet private var toolbarView: UIView! @IBOutlet private var textView: UITextView! 
  2. In viewDidLoad configure the toolbar as an accessory UItextView .

     override func viewDidLoad() { super.viewDidLoad() textView.inputAccessoryView = toolbarView } 

The result is as follows:

enter image description here enter image description here

+76
Dec 09 '16 at 11:12
source share

For quick (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 
+19
Apr 23 '15 at 11:24
source share

You can use this code 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]; } 
+8
May 28 '14 at 7:17
source share

You can use the UITextField property of UITextField

  txtField.inputAccessoryView = toolBar; 
+6
May 28 '14 at 7:07
source share

Swift 3

  let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50)) toolBar.barStyle = UIBarStyle.default toolBar.items = [ UIBarButtonItem(title: "Button1", style: UIBarButtonItemStyle.plain, target: self, action: #selector(test2)), UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil), UIBarButtonItem(title: "Button2", style: UIBarButtonItemStyle.plain, target: self, action: #selector(test1))] toolBar.sizeToFit() myTextField.inputAccessoryView = toolBar 
+3
Aug 25 '16 at 0:39
source share
 textField.inputAccessoryView=[weakSelf addToolBar]; [textField setKeyboardType:UIKeyboardTypeNumberPad]; 

and add 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
Feb 22 '16 at 10:36
source share

In Swift 3 and 4

  let toolBar = UIToolbar() toolBar.barStyle = UIBarStyle.default toolBar.isTranslucent = true toolBar.isUserInteractionEnabled = true toolBar.sizeToFit() toolBar.items = [ UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.sendCodeBtnAction(sender:)))] tfPhone.inputAccessoryView = toolBar 
-one
Jan 31 '18 at 11:08
source share



All Articles