Lib*_*tal 2 uitableview ios autolayout swift
I have tableView size set by AutoLayout (bottom to Bottom Layout Guide, top to another view and so on but first UISearchBar to Top Layout Guide):

Controller with tableView:

I need to change table offset when keyboard is shown so I have these two methods:
// MARK: - Keyboard
func keyboardWasShown (notification: NSNotification) {
let info: NSDictionary = notification.userInfo!
let value: NSValue = info.valueForKey(UIKeyboardFrameBeginUserInfoKey) as! NSValue
let keyboardSize: CGSize = value.CGRectValue().size
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0)
self.tableView.scrollIndicatorInsets = self.tableView.contentInset
}
func keyboardWillBeHidden (notification: NSNotification) {
self.tableView.contentInset = UIEdgeInsetsZero
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero
}
Run Code Online (Sandbox Code Playgroud)
And it's working but I have problem when keyboard is shown. The last item can't be selected and instead of that I get previous item. I tapped where is last item and it should navigate to detail page with last item but instead I see detail page with previous item. It isn't shift for all items but just for the last one and when I filtered to just one item it's working okay. When keyboard is hidden (and items are still filtered) then It's okay too (it selects the right thing). So I guess the problem must be here:
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0)
self.tableView.scrollIndicatorInsets = self.tableView.contentInset
Run Code Online (Sandbox Code Playgroud)
So where could be problem? Thanks for help
我找到了解决方案。我正在使用UIKeyboardWillHideNotification,keyboardWillBeHidden之前调用过method ,didSelectRowAtIndexPath所以contentInset将tableView其设置回UIEdgeInsetsZero,然后出现了错误的indexPath。所以现在我用keyboardDidHide代替keyboardWillBeHidden:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidHide:", name: UIKeyboardDidHideNotification, object: nil)
...
func keyboardDidHide (notification: NSNotification) {
self.tableView.contentInset = UIEdgeInsetsZero
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
823 次 |
| 最近记录: |