2013-10-03 47 views

回答

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; 
} 
+2

This [answer](http://stackoverflow.com/a/19162257/467588)是相似的,但有點短;) – Hlung

12

這裏是一個更簡單方便的方式基於Hlung公司發佈的鏈接做到這一點:

- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView { 

    [tableView setContentInset:UIEdgeInsetsZero]; 
    [tableView setScrollIndicatorInsets:UIEdgeInsetsZero]; 

} 

注:原來的答案使用NSNotificationCenter產生相同的結果。