How can I add a toolbar above the keyboard?

后端 未结 9 1329
孤街浪徒
孤街浪徒 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)
             }
    

提交回复
热议问题