简体   繁体   中英

Swift: Scroll UITableView when keyboard appears

I've found answers to this, but nothing that I could get working for my specific case.

I have a UITableView with dynamic prototyped cells. One of these cells just contains a UITextView , and there is always only one of these and it is always at the very bottom. This is the only text view.

I can't connect this text view to an outlet in my controller because it throws an error (I'm assuming because there can be multiple instances of the text view, and they can't all be mapped to one outlet). So then I can't make my controller conform to the UITextView protocol.

Is there a simple and elegant solution to this?

try to observe the keyboard appearing events in your VC. like this:

NotificationCenter.default.addObserver(self, selector: #selector(scrollToTop(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(scrollToBottom(notification:)), name: .UIKeyboardWillHide, object: nil)

func scrollToTop(notification: NSNotification) {
        //change tableView insets
}


func scrollToBottom(notification: NSNotification) {
          //change tableView insets
 }

You can just set your controller as the textview's delegate in cellForRowAtIndexPath like this

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    ...
    cell.textView.delegate = self
    ...
    return cell
}

Actually your assumption that you cannot make your view controller to conform to UITextView protocol is wrong. You can use custom cell and add attach your outlet to a property in this custom cell. You can then assign tags to the text views and based on tags distinguish between various textViews.

For setup of the delegate of textview in the ViewController you need to set it in the cellForRowAtIndexPath and you can access the delegate method of textview in your viewController.

snippet with swift 3 is here

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    cell.yourTextView.delegate = self //Assign your textView delegate here

}

For scrolling the tableview while keyboard appear/disappear is achieved by the NotificationCenter add the observer for keyboard appear/disappear on viewDidLoad as below

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

You can detect keyboard appear/disappear using below method

func keyboardWillShow(notification: NSNotification) {
     print((notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue.size.height)
}


func keyboardWillHide(notification: NSNotification) {

 }

and don't forgot to remove Observer is disappear method of viewLifeCycle.

there are some third party libraries to achieve this here are some of them

TPKeyboardAvoiding IQKeyboardManager [ Suggestion ] - when you are going to scroll the tableview up side make sure whatever you are insert into the table will be display in table view. keep your tableview in such a way so that whenever you are enter any single content in tableview cell it must be visible by user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM