为与 UITextViews 一起使用的键盘添加第二个 UIToolBar

Adding a 2nd UIToolBar for Keyboard used with UITextViews

我目前有一个可用的 UIToolbar,我在使用 UITextField 时在键盘上方放置了额外的按钮,但我想为上面的 a 创建第二个 UIToolbar 类型UITextView。当我尝试复制我的代码并将其修改为我想要的样子时,它根本没有出现。我不确定我是否做错了什么,或者它是否与我正在使用的 NotificationCenteraddObserver 有关。仅供参考,如果需要的话,整体 class 称为 AddRecipeDirectionVC: UIViewController, UITextViewDelegate

这是我要添加到 viewDidLoad

中的观察者代码
override func viewDidLoad() {
    super.viewDidLoad()

    //...Other Code...

    NotificationCenter.default.addObserver(self, selector: #selector(AddRecipeDirectionVC.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}

最初,我认为我必须在每个 UIViewController 上添加这个观察者,但我开始认为我正在使用的这段代码可以放在初始的 VC 中并且它将在项目的其余部分执行(目前,我在 VC 添加工具栏的地方使用该代码)

接下来,我有 textViewDidBeginEditing 功能,我认为正在添加键盘工具栏。

func textViewDidBeginEditing(_ textView: UITextView) {

    if (textView.textColor == UIColor.lightGray) {
        textView.text = nil
        textView.textColor = UIColor.black
    }
    self.keyboardToolbar(textView: textView) //Adding Keyboard
}

最后,我有 keyboardWillShowkeyboardToolbar 函数

func keyboardWillShow(notification: Notification) {

    let keyFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    let keyHeight = keyFrame.size.height

    self.bottomSpace.constant = keyHeight + 12
}

func keyboardToolbar(textView: UITextView) {

    let toolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
    toolbar.barStyle = UIBarStyle.default
    toolbar.bounds.size.height = 28

    let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)

    let done: UIBarButtonItem = UIBarButtonItem(image:  #imageLiteral(resourceName: "Done Check"), style: UIBarButtonItemStyle.done, target: self, action: #selector(AddRecipeIngredientVC.doneButtonAction))
    done.tintColor = UIColor.aqua(alpha: 1.0)

    let clear: UIBarButtonItem = UIBarButtonItem(title: "Clear", style: UIBarButtonItemStyle.plain, target: self, action: #selector(AddRecipeDirectionVC.clearButtonAction))
    clear.tintColor = UIColor.darkAqua(alpha: 1)

    var items = [UIBarButtonItem]()

    items.append(clear)
    items.append(flexSpace)
    items.append(done)

    toolbar.items = items
    toolbar.sizeToFit()

    textView.inputAccessoryView = toolbar    }

func doneButtonAction() {

    self.directionText.resignFirstResponder()
    self.bottomSpace.constant = 12
}

func clearButtonAction() {

    directionText.text = ""
}

据我所知,我所做的一切都与其他 VC 中的一样,所以它似乎应该工作,但没有任何显示。让我知道是否需要添加任何其他代码。

在此先感谢您提供的任何帮助:)

经过多次谷歌搜索,我发现 keyboardToolbar 代码必须放在 textViewShouldBeginEditing 而不是 textViewDidBeginEditing 中。据我了解,UITextView 布局一旦达到 textViewDidBeginEditing 就已经设置好,因此我没有看到它。这是我最终使用的完成工作的代码。除了下图,其他都一样。

func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {

    self.keyboardToolbar(textView: textView)
    return true
}

func textViewDidBeginEditing(_ textView: UITextView) {

    if (textView.textColor == UIColor.lightGray) {
        textView.text = nil
        textView.textColor = UIColor.black
    }
}

试试这个代码

func keyboardToolbar(textView: UITextView) {

    let toolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
    toolbar.barStyle = UIBarStyle.default
    toolbar.bounds.size.height = 28

    let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)


    let done: UIBarButtonItem = UIBarButtonItem(title: "done", style: UIBarButtonItemStyle.done, target: self, action: #selector(self.doneButtonActionn))
    done.tintColor = UIColor.red

    let clear: UIBarButtonItem = UIBarButtonItem(title: "Clear", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.doneButtonAction))
    clear.tintColor = UIColor.black


    var items = [UIBarButtonItem]()

    items.append(clear)
    items.append(flexSpace)

    items.append(done)

    toolbar.items = items
    toolbar.sizeToFit()

    textView.inputAccessoryView = toolbar


}


func doneButtonAction() {

    self.bodyTextView.resignFirstResponder()
    //self.bottomSpace.constant = 12
}

func doneButtonActionn() {

    self.bodyTextView.resignFirstResponder()
    //self.bottomSpace.constant = 12
}

func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {

    self.keyboardToolbar(textView: textView)
    return true
}