0

我目前有一个工作UIToolbar,我在使用 a 时在键盘上方放置了额外的按钮,UITextField但我想为 aUIToolbar以上制作第二个类型UITextView。当我尝试复制我的代码并将其修改为我希望它的外观时,它根本不显示。我不确定我是否做错了什么,或者它是否NotificationCenteraddObserver我正在使用的和有关。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 中的一样,所以看起来它应该可以工作,但什么都没有出现。让我知道是否需要添加任何其他代码。

提前感谢您提供任何帮助,任何人都可以给我:)

4

2 回答 2

0

试试这个代码

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
}
于 2017-06-19T10:41:38.583 回答
0

经过多次谷歌搜索,我发现keyboardToolbar代码必须放入textViewShouldBeginEditingnot 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
    }
}
于 2017-03-10T02:23:40.703 回答