UITextField
を使用しているときにキーボード上に余分なボタンを置いていますが、UITextView
の2番目のタイプを作成したいと考えていました。私がコードをコピーして、それを見たいと思うように修正しようとしたとき、それはまったく表示されません。私は何かが間違っているのか、それとももし私が使用しているNotificationCenter
とaddObserver
と何か関係があるかどうかはわかりません。参考までに、全体的なクラスはAddRecipeDirectionVC: UIViewController, UITextViewDelegate
と呼ばれています。だからここUITextViewsで使用するキーボード用の2番目のUIToolBarを追加する
は私が各UIViewController
にこのオブザーバーを追加すると思っていた、私はもともと私のviewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
//...Other Code...
NotificationCenter.default.addObserver(self, selector: #selector(AddRecipeDirectionVC.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}
に追加している観測者のコードですが、私は私はこのコードを考え始めています使用して、ちょうど最初の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のと同じで、すべてをやっているので、それが必要のようにそれはそう働いているが、何も現れない。追加のコードを追加する必要がある場合はお知らせください。任意のヘルプ誰も私を与えることができるため:)