2017-03-07 4 views
0

UITextFieldを使用しているときにキーボード上に余分なボタンを置いていますが、UITextViewの2番目のタイプを作成したいと考えていました。私がコードをコピーして、それを見たいと思うように修正しようとしたとき、それはまったく表示されません。私は何かが間違っているのか、それとももし私が使用しているNotificationCenteraddObserverと何か関係があるかどうかはわかりません。参考までに、全体的なクラスは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 
} 

そして最後に、私は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のと同じで、すべてをやっているので、それが必要のようにそれはそう働いているが、何も現れない。追加のコードを追加する必要がある場合はお知らせください。任意のヘルプ誰も私を与えることができるため:)

答えて

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 
    } 
} 
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 
} 
このコードを試してみてください
関連する問題