How can I add a toolbar above the keyboard?

后端 未结 9 1325
孤街浪徒
孤街浪徒 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:04

    Swift 5.0 and above

               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)
             }
    
    0 讨论(0)
  • 2020-11-27 11:08

    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
    
    0 讨论(0)
  • 2020-11-27 11:09

    You do not need to do this in code anymore.

    1. Just simply drag UIView to the top bar of current scene and customize it as you want.

    1. In code simply put IBOutlet for both: toolbarView and textView and make connections.

      @IBOutlet private var toolbarView: UIView!
      @IBOutlet private var textView: UITextView!
      
    2. In viewDidLoad setup your toolbarView as accessoryView of your UItextView.

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

    The result is following:

    0 讨论(0)
  • 2020-11-27 11:10

    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
    
    0 讨论(0)
  • 2020-11-27 11:14
    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;
    

    To 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
    }
    
    0 讨论(0)
  • 2020-11-27 11:15

    You can use the UITextFields inputAccessoryView property

        txtField.inputAccessoryView = toolBar;
    
    0 讨论(0)
提交回复
热议问题