Kenny Ho
Kenny Ho

Reputation: 459

When Keyboard Present, Choose What Causes View To Slide Up

I have a tableView with a textField within row 0 and textView within row 3. My tableview currently slides up every time when the keyboard is present. When the tableView slides up, you can't see the textField within row 0. How do I disable this for row 0 and just keep for row 3? I tried using using Protocol & Delegates to try to encapsulate the function only for row 3, but that doesn't work.

enter image description here class CreateEditItemController: UIViewController, CreateItemDescriptionCellDelegate {

@IBOutlet weak var tableView: UITableView!

func handleKeyboardShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {

        if self.view.frame.origin.y == 0 {
            //self.view.frame.origin.y -= keyboardSize.height
            self.view.frame.origin.y -= 200
        }
    }
}

func handleKeyboardHide(notification: NSNotification) {
    if self.view.frame.origin.y != 0 {
        self.view.frame.origin.y = 0
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch indexPath.row {
    ...
    case 3:
        let cell = tableView.dequeueReusableCell(withIdentifier: "CreateItemDescriptionCell", for: indexPath) as! CreateItemDescriptionCell
        cell.delegate = self
        return cell
    default:
        return tableView.cellForRow(at: indexPath)!
     }
   }
 }


protocol CreateItemDescriptionCellDelegate: class {
    func handleKeyboardShow(notification: NSNotification)
    func handleKeyboardHide(notification: NSNotification)
}

class CreateItemDescriptionCell: UITableViewCell, UITextViewDelegate {
//IBOUTLETS
@IBOutlet weak var notesTextView: UITextView!
weak var delegate: CreateItemDescriptionCellDelegate?

override func awakeFromNib() {
    super.awakeFromNib()
    notesTextView.delegate = self

    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardHide), name: UIResponder.keyboardWillHideNotification, object: nil)

}

@objc func handleKeyboardShow(notification: NSNotification) {
    delegate?.handleKeyboardShow(notification: notification)
}

@objc func handleKeyboardHide(notification: NSNotification) {
    delegate?.handleKeyboardHide(notification: notification)
 }
}

Upvotes: 0

Views: 444

Answers (2)

RichAppz
RichAppz

Reputation: 1529

There are a few routes you can go down, but for simplicities sake, there are a few libraries you can use to get all the calculations down for you and you don't need to worry about it.

One I use all the time is:

TPKeyboardAvoiding - https://github.com/michaeltyson/TPKeyboardAvoiding

IQKeyboardManagerSwift - https://github.com/hackiftekhar/IQKeyboardManager

And many others.

Upvotes: 1

Razi Tiwana
Razi Tiwana

Reputation: 1435

What you are trying to do is possible after some mathematics but i would recommend using a third party pod for this instead of doing this manually on evert controller.

Add this to your pod file:

# IQKeyboardManager: Codeless drop-in universal library allows to prevent issues of keyboard sliding up
# https://github.com/hackiftekhar/IQKeyboardManager
pod 'IQKeyboardManagerSwift'   

For further detail and documentation view: https://github.com/hackiftekhar/IQKeyboardManager

The only line you would have to write would be :

// Enabling IQKeyboardManager
IQKeyboardManager.shared.enable = true

in

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 

This will solve all your problems and you wont have to calculate the frames or anything.

Upvotes: 2

Related Questions