使用TextField使UIToolbar向上移动键盘

时间:2015-10-13 21:27:34

标签: ios xcode swift

我有一个带有TextField和Button作为UIBarButtonItem的UIToolbar。当用户点击工具栏中的TextField时,我试图将此工具栏用作键盘的inputAccessory。

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>'

有什么想法吗?

1 个答案:

答案 0 :(得分:11)

首先要做的事情:

  1. 您应该在Storyboard中创建工具栏约束(左,下,右)。
  2. 在视图控制器中创建底部约束的插座(从故事板中拖动)。保存初始约束值(键盘消失时恢复)
  3. 创建一个观察者,以了解键盘何时出现和消失(3.1并创建一个轻触手势以隐藏键盘)
  4. 当键盘4.1出现且4.2消失时,您只会更改底部约束值(键盘大小)。您也可以为工具栏设置动画。
  5. 类似的东西:

    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
        }
    
    }
    

    希望它有所帮助!