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

问题描述:

我目前有一个工作UIToolbar当我使用UITextField时在键盘上方放置额外的按钮,但我想要制作第二个UIToolbar类型的UITextView以上。当我试图复制我的代码并将其修改为我想要的样子时,它完全不显示。我不确定自己是否做错了什么,或者是否与我使用的NotificationCenteraddObserver有关。仅供参考,如果需要,整个班级被称为AddRecipeDirectionVC: UIViewController, UITextViewDelegate添加与UITextViews一起使用的键盘的第二个UIToolBar

因此,这里是观察者码我添加到我的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代码必须放在textViewShouldBeginEditingtextViewDidBeginEditing。据我所知,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 
}