【问题标题】:How can I add a toolbar above the keyboard?如何在键盘上方添加工具栏?
【发布时间】:2014-07-17 06:38:53
【问题描述】:

我以编程方式创建了一个UIToolBar 并在其上添加了一个UITextField。现在,当我单击另一个文本字段时,我需要该工具栏位于键盘上方。

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

【问题讨论】:

标签: ios uitextfield uitoolbar


【解决方案1】:
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;

关闭键盘:

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

斯威夫特 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

斯威夫特 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
}

【讨论】:

  • 或者可以使用故事板来完成,例如here
  • 您可以通过 UITextField.appearance().inputAccessoryView = /* your tool bar setup code */ 将其添加到所有文本字段中
  • 在swift 4选择器中应该是这样的: UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(self.OnTapDone(_:)))
  • 工具栏宽度应该根据屏幕尺寸而不是视图尺寸来设置,现在编辑它以及关闭键盘的方法
【解决方案2】:

您不再需要在代码中执行此操作。

  1. 只需将 UIView 拖到当前场景的顶部栏并根据需要进行自定义即可。

  1. 在代码中,只需为 toolbarView 和 textView 输入 IBOutlet 并建立连接。

    @IBOutlet private var toolbarView: UIView!
    @IBOutlet private var textView: UITextView!
    
  2. 在viewDidLoad 中将您的工具栏视图设置为您的UItextView 的附件视图。

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

结果如下:

【讨论】:

  • 哇。多年来,我一直在努力做到这一点;人们说故事板没用!
  • 正如豪斯先生所说:人们会说谎;)
  • 这太不可思议了!!效果很好。
  • 不知道你可以这样做。太棒了!
  • 对于 alertView 输入文本,你可以使用这个:alert.textFields?.first?.inputAccessoryView = toolbarView
【解决方案3】:

对于 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

【讨论】:

    【解决方案4】:

    您可以使用此代码,它对我有用。

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

    【讨论】:

      【解决方案5】:

      您可以使用UITextFields inputAccessoryView 属性

          txtField.inputAccessoryView = toolBar;
      

      【讨论】:

        【解决方案6】:

        斯威夫特 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
        

        【讨论】:

          【解决方案7】:

          Swift 5.0 及更高版本

                     let toolBar = UIToolbar(frame: CGRect(x: 0.0,
                                                            y: 0.0,
                                                            width: UIScreen.main.bounds.size.width,
                                                            height: 44.0))//1
                      let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)//2
                      let DoneButton = UIBarButtonItem(title: "Done", style: .plain, target: target, action: #selector(tapDone))//3
                      toolBar.setItems([flexibleSpace, DoneButton], animated: false)
                     textField.inputAccessoryView = toolBar
                 // Done Action
                  @objc func tapDone() {
                     self.view.endEditing(true)
                   }
          

          【讨论】:

            【解决方案8】:
            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;
            }
            

            【讨论】:

            • 第一部分是在键盘顶部添加工具栏。第二个是创建工具栏
            • 感谢您对 ios 的关注
            • @DURGESHKUMAR 你能解释一下这条线吗? textField.inputAccessoryView=[weakSelf addToolBar];
            【解决方案9】:

            在 Swift 3 和 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
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-11-20
              • 1970-01-01
              • 1970-01-01
              • 2020-11-11
              • 2016-12-20
              • 2011-03-27
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多