我目前有一个正常工作UIToolbar
,我在使用UITextField
时在键盘上方放置了额外的按钮,但我想要在第二个UIToolbar
类型之上UITextView
。当我尝试复制我的代码并将其修改为我希望它看起来的样式时,它根本不会显示出来。我不确定我做错了什么,或者是否与我使用的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中,它会随身携带对于项目的其余部分(目前,我在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 = ""
}
据我所知,我做的一切与其他VC一样,所以看起来它应该有效,但什么都没有显示出来。如果需要添加任何其他代码,请与我们联系。
感谢您提前获得任何人可以给予我的帮助:)
答案 0 :(得分:0)
经过大量谷歌搜索,我发现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
}
}
答案 1 :(得分: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
}