Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moving View up with textfield and Button when keyboard appear Swift

Tags:

xcode

ios

swift

I know there are a few posted questions on this topic, but none of them worked. I have a tableview and under it there's a UIView that contains a textfield and button. When my keyboard appears, it covers the whole UIView, making me unable to click "Send". The image to show how my app looks like :

enter image description here

at the moment, my codes are:

 override func viewDidLoad() {
    super.viewDidLoad()
    inputTextField.delegate = self
    inputTextField.layer.cornerRadius = 5
    inputTextField.clipsToBounds = true
    sendButton.layer.cornerRadius = 5
    sendButton.clipsToBounds = true
    self.tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "TableViewCell")
    loadMsg()
    self.hideKeyboardWhenTappedAround()

}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    inputTextField.becomeFirstResponder()
}

How should I move the UIView up along with the textfield and button that is contained by the UIView when my keyboard appears?

like image 796
William Loke Avatar asked Dec 06 '22 11:12

William Loke


2 Answers

Just add this library into your project using POD and thats it. It will automatically do it. You have not to do anything else.

Add pod like that

pod 'IQKeyboardManagerSwift'

In AppDelegate.swift, just import IQKeyboardManagerSwift framework and enable IQKeyboardManager.

import IQKeyboardManagerSwift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

  IQKeyboardManager.shared.enable = true

  return true
 }
}

For reference please have a look on this url, https://github.com/hackiftekhar/IQKeyboardManager

like image 166
aBilal17 Avatar answered May 23 '23 23:05

aBilal17


Here is solution, Create bottom layout constraint reference of send button view

@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
@IBOutlet weak var sendbuttonView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

    override func viewWillDisappear(_ animated: Bool) {
        NotificationCenter.default.removeObserver(self)
    }

    @objc func handleKeyboardNotification(_ notification: Notification) {

    if let userInfo = notification.userInfo {

        let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue

        let isKeyboardShowing = notification.name == NSNotification.Name.UIKeyboardWillShow

        bottomConstraint?.constant = isKeyboardShowing ? -keyboardFrame!.height : 0

        UIView.animate(withDuration: 0.5, animations: { () -> Void in
            self.view.layoutIfNeeded()
        })
    }
}

Demo example

like image 40
iParesh Avatar answered May 24 '23 01:05

iParesh