我当前有一个工作UIToolbar
,在使用UITextField
时,我在键盘上方放置额外的按钮,但我想为上述UITextView
制作第二个UIToolbar
类型。当我尝试复制代码并将其修改为我希望它看起来的样子时,它根本不会出现。我不确定我是否做错了什么,或者是否与我正在使用的NotificationCenter
和addObserver
有关。仅供参考,整个类别(如果完全需要)称为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中,并且它将在项目的其余部分中进行(目前,我正在Ever VC中使用该代码,我正在添加工具栏)
接下来,我有textViewDidBeginEditing
功能,我认为正在添加键盘工具栏。
func textViewDidBeginEditing(_ textView: UITextView) {
if (textView.textColor == UIColor.lightGray) {
textView.text = nil
textView.textColor = UIColor.black
}
self.keyboardToolbar(textView: textView) //Adding Keyboard
}
,最后,我有keyboardWillShow
和keyboardToolbar
功能
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 = ""
}
据我所知,我正在做与其他风险投资相同的事情,因此看起来应该可以正常工作,但是什么都没有出现。让我知道是否需要添加任何其他代码。
感谢您在Advanced中提供的任何帮助:)
大量谷歌搜索后,我发现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
}