2013-10-03 2 views
8

У меня есть UISearchDisplayController, и он отображает результаты в виде таблицы. Когда я пытаюсь прокрутить таблицу, содержимое будет точно _keyboardHeight выше, чем должно быть. Это приводит к ложному нижнему смещению. Есть> 50 пунктов в Tableview, так что не должно быть пустое пространство, как показано нижеUISearchDisplayController Недопустимое смещение содержимого табличного представления после скрытия клавиатуры

enter image description here

ответ

12

Я решил эту проблему, добавив NSNotificationCenter слушателя

- (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

Этот [ответ] (http://stackoverflow.com/a/19162257/467588) похож, но немного короче;) – Hlung

12

Вот более простой и удобный способ сделать это на основе публикуемой ссылки Hlung в:

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

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

} 

Примечания: Оригинальный ответ использует NSNotificationCenter для получения тех же результатов.

Смежные вопросы