-1

i have the same question as this : iPhone Keyboard with accessory view height problems

but the answer has no new solution and does not solve anything!

Mehrdad
  • 613
  • 1
  • 9
  • 18

1 Answers1

0

I found a good solution Here

Made few changes and updated it to swift 4.2.

Few points to be mentioned

  1. Created an outlet of textfield and bottom constraint from Storyboard to ViewController
  2. Bottom constraint is used for moving the textfield up and down.
class ViewController: UIViewController {

    @IBOutlet weak var inputField: UITextField!

    @IBOutlet weak var textFieldBottomContraint: NSLayoutConstraint!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUpKeyBoardNotifications()
        self.addToolBarTo(uiElement: self.inputField)
    }
    func setUpKeyBoardNotifications()
    {
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.keyboardNotification(notification:)),
                                               name: UIResponder.keyboardWillChangeFrameNotification,
                                               object: nil)
    }
    func addToolBarTo(uiElement element:UITextField)
    {
        let numberToolbar = UIToolbar(frame:CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 45))
        numberToolbar.barStyle = .black
        numberToolbar.items = [
            UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(ViewController.cancelAction)),
            UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
            UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ViewController.doneAction))]
        numberToolbar.sizeToFit()
       element.inputAccessoryView = numberToolbar

    }
    @objc func keyboardNotification(notification: NSNotification) {
        if let userInfo = notification.userInfo {
            let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
            let endFrameY = endFrame?.origin.y ?? 0
            let duration:TimeInterval = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
            let animationCurveRawNSN = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
            let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
            let animationCurve:UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)
            if endFrameY >= UIScreen.main.bounds.size.height {
                self.textFieldBottomContraint?.constant = 0.0
            } else {
                self.textFieldBottomContraint?.constant = endFrame?.size.height ?? 0.0
            }
            UIView.animate(withDuration: duration,
                           delay: TimeInterval(0),
                           options: animationCurve,
                           animations: { self.view.layoutIfNeeded() },
                           completion: nil)
        }
    }
    @objc func cancelAction()
    {
        self.inputField.resignFirstResponder()
    }
   @objc func doneAction()
    {
        self.inputField.resignFirstResponder()
    }
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}