2015-10-13 190 views
2

我有一個帶有TextField和Button的UIToolbar作爲UIBarButtonItem。當用戶點擊工具欄內的TextField時,我試圖將此工具欄用作鍵盤的inputAccessory。使TextField的UIToolbar向上移動鍵盤

enter image description here

我發現this question,試圖解決同樣的問題。不幸的是,解決方案並不有效。

我想要的是:

class ChatViewController: UIViewController, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet weak var chatTableView: UITableView! 
    @IBOutlet weak var chatToolbar: UIToolbar! 

    @IBOutlet weak var textFieldBarButtonItem: UIBarButtonItem! 

    override func viewDidAppear(animated: Bool) { 
     super.viewDidLoad() 
     self.chatTableView.delegate = self 
     self.chatTableView.dataSource = self 
     self.chatToolbar.removeFromSuperview() 

    } 

    override var inputAccessoryView: UIView{ 
     get{ 
      return self.chatToolbar 
     } 
    } 

    override func canBecomeFirstResponder() -> Bool { 
     return true 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 

     let cell = UITableViewCell() 
     return cell 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return 0 
    } 

} 

什麼我得到的回覆是:

*** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 
'child view controller:<UICompatibilityInputViewController: 0x13ff34e00> should have parent view controller: 
<App.ChatViewController: 0x13ff21cc0> but requested parent is:<UIInputWindowController: 0x1400b4600>' 

任何想法?

+0

這不是一個壞的方法,問題應該是'inputAccessoryView:'在'viewDidAppear'之前被調用。附件視圖不應該屬於任何視圖層次結構。順便說一句:接受的答案也沒關係 – Omer

+0

@Omer太棒了,你有什麼建議? – adolfosrs

+1

我認爲這兩種方法都很好,我更喜歡accessoryInputView,因爲它在其他好處(如:更簡單的使用'UIScrollViewKeyboardDismissModeInteractive',故事板/獨立於xib,約束瘋狂free = D的方法)之間更乾淨。下面的鏈接包含一個obj-c proj,其基本實現使用了可以演變的附件視圖方法,以防萬一您想嘗試一下:https://www.dropbox.com/s/a4bsvz0ie95i3qn/KeyboardAccessory.zip? dl = 0 |||不是我使用工具欄,但你可以使用任何你想要的UIView子類 – Omer

回答

10

首先第一件事情:

  1. 你應該在故事板創建約束工具欄(左,下,右)。
  2. 在視圖控制器中創建底部約束的出口(從故事板拖動)。保存最初的約束條件值(當鍵盤消失恢復)
  3. 當你的鍵盤出現和消失創建一個觀察者知道(3.1,並創建一個敲擊姿態隱藏鍵盤)
  4. 當鍵盤4.1出現和4.2消失了,你會只改變底部約束值(鍵盤大小)。您也可以爲工具欄設置動畫。

喜歡的東西:

class ChatViewController: UIViewController, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet weak var chatTableView: UITableView! 
    @IBOutlet weak var chatToolbar: UIToolbar! 

    @IBOutlet weak var textFieldBarButtonItem: UIBarButtonItem! 

    //2 
    @IBOutlet weak var toolbarBottomConstraint: NSLayoutConstraint! 
    var toolbarBottomConstraintInitialValue: CGFloat? 

    override func viewDidAppear(animated: Bool) { 
     super.viewDidAppear(animated) 
     self.chatTableView.delegate = self 
     self.chatTableView.dataSource = self 
     self.chatToolbar.removeFromSuperview() 

     //2 
     self.toolbarBottomConstraintInitialValue = toolbarBottomConstraint.constant 
     //3 
     enableKeyboardHideOnTap() 

    } 

    // 3 
    // Add a gesture on the view controller to close keyboard when tapped 
    private func enableKeyboardHideOnTap(){ 

     NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil) // See 4.1 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil) //See 4.2    

     // 3.1 
     let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "hideKeyboard") 

     self.view.addGestureRecognizer(tap) 
    } 

    //3.1 
    func hideKeyboard() { 
     self.view.endEditing(true) 
    } 

    //4.1 
    func keyboardWillShow(notification: NSNotification) { 

     let info = notification.userInfo! 

     let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 

     let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double   

     UIView.animateWithDuration(duration) {() -> Void in 

      self.toolbarBottomConstraint.constant = keyboardFrame.size.height + 5 

      self.view.layoutIfNeeded() 

     } 

    } 

    //4.2 
    func keyboardWillHide(notification: NSNotification) { 

     let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double 

     UIView.animateWithDuration(duration) {() -> Void in 

      self.toolbarBottomConstraint.constant = self.toolbarBottomConstraintInitialValue! 
      self.view.layoutIfNeeded() 

     } 

    } 

    override var inputAccessoryView: UIView{ 
     get{ 
      return self.chatToolbar 
     } 
    } 

    override func canBecomeFirstResponder() -> Bool { 
     return true 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 

     let cell = UITableViewCell() 
     return cell 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return 0 
    } 

} 

希望它能幫助!

+0

工作就像一個魅力。你是男人。 – adolfosrs

+0

工作完美! –