2014-01-15 7 views
4

Как можно добавить два простых UILabels в TableHeaderView и сохранить поведение поиска по умолчанию контроллера отображения в iOS7?Добавление subviews в tableHeaderView с UISearchBar в iOS7 misbehaves

Или, визуально:

Почему этот код:

UIView *tableHeadView = self.tableView.tableHeaderView; 

UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 36, 320, 30)]; 
[tableHeaderLabel setTextAlignment:NSTextAlignmentCenter]; 
tableHeaderLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:18]; 
tableHeaderLabel.text = @"Questions" 

UILabel *tableHeaderPrononce = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 320, 30)]; 
[tableHeaderPrononce setTextAlignment:NSTextAlignmentCenter]; 
tableHeaderPrononce.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15]; 
tableHeaderPrononce.text = @"test test test test"; 

[tableHeadView addSubview:tableHeaderLabel]; 
[tableHeadView addSubview:tableHeaderPrononce]; 

добавлен к событию UITableViewController viewDidLoad (который содержит UISearchDisplayerController) дает этот прекрасный результат в iOS6:
Normal While searching

и этот ужасный ужасный результат i п iOS7:
Normal While searching

Поведение: При нормальном режиме UILabels я добавил не показаны. в то время как поиск активен, UILabels внезапно появляются в верхней части ячеек таблицы и не прокручиваются.

Кроме того, я получаю сбои во время поиска в iOS 7, которые никогда не происходили на iOS6. Вероятно, это не связано с этой частью кода, но, тем не менее, я должен был упомянуть об этом.

Я пробовал все, что мог найти в этом вопросе, чтобы исправить эту проблему, но всегда что-то ломается или исчезает (в основном UISearchBar).

Помощь

+0

Если бы какой-либо способ связать проект с github, я бы рассмотрел обе проблемы для вас. – Mike

+0

Его нет на git, но все соответствующие коды здесь – mindbomb

ответ

7

Посмотрите чувак, прежде чем изменять свой код или получить разочарование в связи с поведением iOS7, рекомендуется пройти через UI Transition Guide дается яблоко.

После прочтения этого вопроса вы узнаете, почему сгруппированный стиль tableView выглядит раздражающим, отнюдь не раздражает с точки зрения ios7. Каждая группа расширяет всю ширину экрана.

Кроме того UISearchBar поведение можно контролировать, если вы читаете через Search Bar and Scope Bar

Я надеюсь, что помогает. Если вам нужен весь код, сообщите нам об этом. мы можем предоставить образец.

Вот пример кода, как Обязательный Добавление UISearchBar для просмотра, а не TableViewHeader

  • Добавление UISearchBar в self.view

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f,0.0f, 320, 44.0f)]; 
    searchBar.delegate   = self; 
    searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
    searchBar.searchBarStyle = UISearchBarStyleMinimal; 
    [self.view addSubview:searchBar]; 
    
  • Добавление UITableView в self.view

    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain]; 
    tableView.delegate   = self; 
    tableView.dataSource  = self; 
    tableView.backgroundColor = [UIColor clearColor]; 
    tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); 
    [self.view addSubview:tableView]; 
    
  • Создание и добавление UITableView.tableHeaderView

    UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 15)]; 
    [tableHeaderLabel setTextAlignment:NSTextAlignmentCenter]; 
    tableHeaderLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:18]; 
    tableHeaderLabel.text = @"Questions"; 
    tableHeaderLabel.alpha = 0.9; 
    
    UILabel *tableHeaderPrononce = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 320, 15)]; 
    [tableHeaderPrononce setTextAlignment:NSTextAlignmentCenter]; 
    tableHeaderPrononce.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15]; 
    tableHeaderPrononce.text = @"test test test test"; 
    tableHeaderPrononce.alpha = 0.7; 
    
    UIView *aHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)]; 
    [aHeaderView setBackgroundColor:[UIColor clearColor]]; 
    [aHeaderView addSubview:tableHeaderLabel]; 
    [aHeaderView addSubview:tableHeaderPrononce]; 
    tableView.tableHeaderView = aHeaderView; 
    

Вот результаты:

Normal Appearance

SearchBar

SearchDisplayCon Троллингист

// Create search controller 
searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; 
searchController.searchResultsDataSource = self; 
searchController.searchResultsDelegate = self; 
searchController.delegate    = self; 
+0

Так выглядит, что вам нужен код. Я дам вам код за какое-то время. Благодарю. –

+1

Посмотрите на мое редактирование. Я добавлю дополнительный код, если вы хотите, чтобы UISearchBar был добавлен как часть tableHeaderView. –

+0

ОК, круто спасибо! Вы создали автономный UISearchBar, где моя связана с UISearchDisplayController. Кроме того, он уже появляется на TableViewHeader, если я не добавил эти 2 UILabels. – mindbomb

1

Вы можете сделать это просто метод UITableViewDelegate показано ниже: -

(если вам это нравится, или она работает, пожалуйста, на голосование его. Happy Coding.)

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 

    UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)]; 
    sectionHeaderView.backgroundColor = [UIColor grayColor]; 
    UILabel *headerLabel = [[UILabel alloc] init]; 
    [sectionHeaderView addSubview:headerLabel]; 

    return sectionHeaderView; 
} 
Смежные вопросы