1

Here I used Custom UITableviewCell and each cell having multiple UITextfield and it's look like as Cardview.

When I click add icon new Cardview will be added. Here I don't know how to handle Autoscroll option when we click on UITextfield.

Please find below Image:

enter image description here

Kathiresan Murugan
  • 2,783
  • 3
  • 23
  • 44
Venkatesh G
  • 354
  • 1
  • 4
  • 10
  • 2
    use IQKeyBoardManager – Saad Chaudhry Feb 22 '18 at 08:05
  • 1
    Add observer to keyboard keyboardWillShow/DidShow notification, find the size of key board from the dictionary of notification and then set the content offset of your tableView to scroll your tableView. Take a look at https://stackoverflow.com/questions/594181/making-a-uitableview-scroll-when-text-field-is-selected – Sandeep Bhandari Feb 22 '18 at 08:20
  • https://blog.apoorvmote.com/move-uitextfield-up-when-keyboard-appears/ – Vincent Joy Feb 22 '18 at 08:25

2 Answers2

2

No need any calculations, use below code it will work perfectly,

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

 func keyboardWillShow(_ notification:Notification) {

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        reimbursementTableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0)
    }
}

func keyboardWillHide(_ notification:Notification) {

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        reimbursementTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
    }
}
Venkatesh G
  • 354
  • 1
  • 4
  • 10
2

API has changed a little bit with Swift 4.2. You need to do something similar to this.

//inside viewDidLoad
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)

then we'll have to the supporting functions. Since we are involving selectors (which is part of Objective C stuff) we need to use @objc prefix with our functions.

@objc func keyboardWillShow(_ notification:Notification) {

        if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
        }
}

@objc func keyboardWillHide(_ notification:Notification) {

        if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        }
}

if you have a different name for your UITableView then change the variable tableView inside the function.

Also, if you want to hide the keyboard when the user touches outside the TextView or TextField then do the following.

//inside the viewDidLoad
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
view.addGestureRecognizer(tap)

and implement the function given in the selector as below

@objc func dismissKeyboard() {
    view.endEditing(true)
}
Jay Mayu
  • 17,023
  • 32
  • 114
  • 148