如何在键盘上添加ToolBar?

我已经编程创build了一个工具栏,并在其上添加了一个UITextField 。 现在,我需要在另一个文本框中点击ToolBar的时候在键盘上方。

 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]; 
 UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.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; 

使用Swift:

 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: "cancelNumberPad"), UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil), UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneWithNumberPad")] numberToolbar.sizeToFit() phonenumberTextField.inputAccessoryView = numberToolbar 

你不需要在代码中做到这一点。

  1. 只需将UIView拖动到当前场景的顶部栏,然后根据需要进行自定义。

在这里输入图像说明

  1. 在代码中简单的把IBOutlet放到toolbarViewtoolbarView ,并build立连接。

     @IBOutlet private var toolbarView: UIView! @IBOutlet private var textView: UITextView! 
  2. viewDidLoad设置您的toolbarView作为你的UItextView

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

结果如下:

在这里输入图像说明 在这里输入图像说明

对于迅捷(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 

你可以使用这个代码为我工作。

 -(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]; } 

你可以使用UITextFieldinputAccessaryView属性

  txtField.inputAccessoryView = toolBar; 

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 
 textField.inputAccessoryView=[weakSelf addToolBar]; [textField setKeyboardType:UIKeyboardTypeNumberPad]; 

并添加一个方法

 -(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; }