Keep the keyboard away from the lock cell

My application displays a form, and some cells have text fields where you can enter information. If the cell is at the bottom of the screen, the cell will be completely locked, which means that you cannot understand what you are responding to. My UITableView is built into the UIViewController.

I looked through many topics that offer answers to this problem and implemented some of the code. If I can get this method to work, I believe that my code will work:

func textFieldDidBeginEditing(_ textField: UITextField) {
    if (self.tableView.contentOffset.y == 0)
    {
        self.tableView.scrollToRow(at: self.tableView.indexPath(for: ), at: UITableViewScrollPosition, animated: true)
    }
}

Now I'm trying to solve the problem of getting an active cell so that it can be entered into self.tableView.indexPath(for: ).

How should I do it? Is this the right way to move the view above the keyboard?

+4
5

NotificationCenter.

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillAppear), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillDisappear), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
+1


iOS

, tableView - .

func textFieldDidBeginEditing(_ textField: UITextField) {
    tableView.isScrollEnabled = false
    var path: IndexPath?

    path = theIndexPathOfTheCellWithTheTextField

    if path != nil {
        let rect = CGRect(x: 0, y: 0, width: tableView.frame.width, height: tableView.frame.height)
        tableView.tableFooterView = UIView(frame: rect)

        UIView.animate(withDuration: 0.6, animations: {
            [weak self] in
            self?.tableView.scrollToRow(at: path!, at: .top, animated: false)
        }, completion: nil)
    }
}

func textFieldDidEndEditing(_ textField: UITextField) {
    tableView.tableFooterView = UIView()
    tableView.isScrollEnabled = true
}

tableView tableFooterView, .

EDIT - : indexPath, . , , :

//This works where neither theRowInt or theSectionInt are negative.
IndexPath(row: theRowInt, section: theSectionInt)

//So this would be the first section/first row
IndexPath(row: 0, section: 0)

, , textField:

let textFieldCell = textField.superview.superview.superview as! UITableViewCell
let indexPath = self.tableView.indexPathForCell(textFieldCell)

, , - . textField contentView, iOS 7. , .superviews, .

, , . - .

0

UITableViewController, UIViewController tableView.

. , , UITableViewController, , , . , UITableViewController , .

0

WillAppear UIKeyboardWillShow UIKeyboardWillHide:

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

, View

func keyboardWillShow(notification: Notification) {
     guard let keyboardFrame = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue else { return }
       let convertedFrame = view.convert(keyboardFrame, from: nil)
       tableView.contentInset.bottom = convertedFrame.height
}

keyboardWillHide 0 View . , .

 func keyboardWillHide(notification: Notification) {
    tableView.contentInset.bottom = 0
    tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
}

, textField , .

, - , , , , viewWillDisappear deinit .

  deinit {
    NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillHide, object: nil)

     // If you don't have any other notifications you wan't to keep observing you can remove yourself from all observers by using this one line
     // NotificationCenter.default.removeObserver(self)
  } 
0

Source: https://habr.com/ru/post/1662172/


All Articles