我有一個UISearchDisplayController,它在tableview中顯示結果。當我嘗試滾動tableview時,內容大小恰好是_keyboardHeight比它應該高。這會導致一個錯誤的底部偏移量。有>在tableview中50個項目,所以不應該通過增加NSNotificationCenter
監聽器是一個空格,如下鍵盤隱藏後UISearchDisplayController tableview內容偏移量不正確
8
A
回答
12
我解決了這個
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
//this is to handle strange tableview scroll offsets when scrolling the search results
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
}
不要忘記刪除監聽器
- (void)searchDisplayController:(UISearchDisplayController *)controller willHideSearchResultsTableView:(UITableView *)tableView {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
}
調整的tableview contentsize在通知方法
- (void)keyboardDidHide:(NSNotification *)notification {
if (!self.searchDisplayController.active) {
return;
}
NSDictionary *info = [notification userInfo];
NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize KeyboardSize = [avalue CGRectValue].size;
CGFloat _keyboardHeight;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIDeviceOrientationIsLandscape(orientation)) {
_keyboardHeight = KeyboardSize.width;
}
else {
_keyboardHeight = KeyboardSize.height;
}
UITableView *tv = self.searchDisplayController.searchResultsTableView;
CGSize s = tv.contentSize;
s.height -= _keyboardHeight;
tv.contentSize = s;
}
12
這裏是一個更簡單方便的方式基於Hlung公司發佈的鏈接做到這一點:
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
[tableView setContentInset:UIEdgeInsetsZero];
[tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
}
注:原來的答案使用NSNotificationCenter產生相同的結果。
相關問題
- 1. 奇怪UISearchDisplayController崩潰隱藏鍵盤
- 2. 編輯內容時隱藏鍵盤UIWebView
- 3. 在屏幕鍵盤上隱藏內容
- 4. 鍵盤隱藏Android內容React Native webview
- 5. iOS UISearchBar的鍵盤部分隱藏tableView
- 6. 辭職急救員隱藏鍵盤,但不生成鍵盤將隱藏/鍵盤確實隱藏事件
- 7. 隱藏UISearchDisplayController的UISearchBar
- 8. UIScrollView的內容偏移正在重置
- 9. 隱藏鍵盤
- 10. 滾動條隱藏在鍵盤後面
- 11. 隱藏鍵盤
- 12. 我們搜索後用TableView滾動時隱藏鍵盤
- 13. 鍵盤隱藏後QuickAction錨點
- 14. 永久隱藏UISearchDisplayController
- 15. 按Enter鍵後隱藏鍵盤
- 16. 更改內容偏移量
- 17. 隱藏軟鍵盤
- 18. 防止UISearchDisplayController隱藏UiTable
- 19. becomeFirstResponder不隱藏鍵盤
- 20. UIPickerView:鍵盤不隱藏
- 21. UIWebView不會隱藏鍵盤
- 22. 按下按鈕後隱藏鍵盤
- 23. 隱藏移動內容
- 24. 隱藏鍵盤/ resignFirstResponder強制
- 25. jQuery隱藏/顯示偏移量問題
- 26. UITableViewCell不需要的內容偏移量
- 27. 隱藏在鍵盤後面的UITextField
- 28. 鍵盤隱藏UIView iOS
- 29. 爲什麼pytz偏移量不正確?
- 30. uitextfield隱藏鍵盤?
This [answer](http://stackoverflow.com/a/19162257/467588)是相似的,但有點短;) – Hlung