This is what I want (cell top become at table top)
This is what is happening (cell top become at table center)
I use this code
let indexPath = NSIndexPath(forRow: 2+numberOfComments, inSection: 0)
self.tableView?.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Top, animated: true)
EDIT :- Extra info, the target cell is the last cell in the table.
And this is the code to get the height of keyboard
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.onShowKeyboard(_:)), name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.onHideKeyboard), name: UIKeyboardDidHideNotification, object: nil)
func onShowKeyboard(notification:NSNotification) {
let userInfo:NSDictionary = notification.userInfo!
let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.CGRectValue()
let keyboardHeight = keyboardRectangle.height
botMargin.constant = keyboardHeight
}
func onHideKeyboard() {
botMargin.constant = 0
}
Have you try this, First declare one CGPoint
instance to store current contentOffset
of tableview.
var lastScrollPoint = CGPoint()
Now use this code to scroll the tableview
let cell = tableView.cellForRowAtIndexPath(indexPath)
let scrollPoint = CGPointMake(0, cell.frame.origin.y)
self.lastScrollPoint = self.tableView.contentOffset;
self.tableView.setContentOffset(scrollPoint, animated: true)
Now on textFieldShouldReturn
set the scroll to default or the lastScroll
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.tableView.setContentOffset(self.lastScrollPoint, animated: true)
return true
}
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.