ios - 在窗口的中间,UISearchDisplayController.displaysSearchBarInNavigationBar 定位搜索栏

  显示原文与译文双语对照的内容
0 0

我试着使用 displaysSearchBarInNavigationBar属性在iOS7 UISearchDisplayController类来显示搜索栏出现在导航条。

使用 AdvancedTableSearch示例来自Apple 作为基础,我修改了代码不允许禁用作用域( 导航栏) 和集中 displaysSearchBarInNavigationBar设置为true,如下所示。

- (void)viewDidLoad
{
   [super viewDidLoad];
   // create a mutable array to contain products for the search results table
   self.searchResults = [NSMutableArray arrayWithCapacity:[self.products count]];
   self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
}

不幸的是我去结果如下所示:

search bar in the wrong place

搜索栏出现在屏幕的中间而不是在navigationItem 。

我哪做的不对?

ps : 我不确定它是否有关,但是 self.searchDisplayController.navigationItem属性为 nil.

时间:原作者:2个回答

0 0

从表中删除搜索栏视图。 使用 displaysSearchBarInNavigationBar意味着UISearchDisplayController来替你照顾放置层次结构中的搜索栏。

另外, navigationItem总是nil,直到 displaysSearchBarInNavigationBar设置为YES 。 在需要时,才会创建的项。

原作者:
0 0

我出现了同样的问题和你一样,花了个小时来寻求解决方案。 Ultimately我跟创建 UISearchDisplayController通过编程的方式。

我创建了一个 SearchTableViewControllerStoryboard再通过编程完成剩下的工作。 控制器中的代理必须包含标题,你要注意到在下面提供的代码。 ,有趣的事情发生在编制。 searchBar. 请注意我没有设置它的delegate? 。 UISearchDisplayController这么做对我来说,当我创建它的使用 searchBar. 那我只需要设置delegate和来源。 UISearchDislpayController和用于 searchResults我这么好的,请在创建控制器。 我无法向你提供答案,以" 为什么" searchBar中创建时设置视图的中心, Storyboard并设置为 displaysSearchBarInNavigationBar:YES在代码中找到,但是我也遇到了相同的问题,以下是一个可行的解决方案。 尤其是考虑我什么都不用大小为= = = = =

SearchTableViewController h

#import <UIKit/UIKit.h>
@interface SearchableTableViewController : UITableViewController<UISearchDisplayDelegate>
//I only need the SearchDisplayController Delegate because it magically has all the needed child delegates. :)
@end

SearchTableViewController m

#import "SearchDisplayController.h"
@property (strong,nonatomic)  IBOutlet UITableView *acSearchTableView;
@property (retain,nonatomic)  UISearchBar *acSearchBar;
@property (retain,nonatomic)  UISearchDisplayController *searchDsplyCntrl;
@property (strong,nonatomic)  NSArray *unfilteredResults;
@property (strong,nonatomic)  NSMutableArray *filteredResults;
@implementation SearchTableViewController 
- (void) viewDidLoad {
    [super viewDidLoad];
    _acSearchBar = [[UISearchBar alloc]init];
    _acSearchBar.showsCancelButton = NO;
    /* NOTE: by default the placholer is centered. It can be left aligned with spaces */
    _acSearchBar.placeholder = @"Search                                                      ";
    _searchDsplyCntrl = [[UISearchDisplayController alloc]initWithSearchBar:_acSearchBar contentsController:self];
    _searchDsplyCntrl.delegate = self;
    _searchDsplyCntrl.searchResultsDelegate = self;
    _searchDsplyCntrl.searchResultsDataSource = self;
    _searchDsplyCntrl.displaysSearchBarInNavigationBar = YES;
    _unfilteredResults = [[NSArray alloc]initWithObjects:
                                               [ResultObj resultWithName:@"first"],
                                               [ResultObj resultWithName:@"second"],
                                               [ResultObj resultWithName:@"third"],
                                               [ResultObj resultWithName:@"forth"],
                                               [ResultObj resultWithName:@"fifth"],
                                               [ResultObj resultWithName:@"sixth"],
                                               [ResultObj resultWithName:@"seventh"],
                                               [ResultObj resultWithName:@"eigth"],nil];
    _filteredResults = [NSMutableArray arrayWithCapacity:[_unfilteredResults count]];
    [_acSearchTableView reloadData];
}
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    [_searchDsplyCntrl setActive:YES animated:YES];
    [_searchDsplyCntrl.searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
    [_searchDsplyCntrl setActive:NO animated:YES];
    [_searchDsplyCntrl.searchBar setShowsCancelButton:NO animated:YES];
}

A 搜索栏显示在导航栏不能为范围栏。

在showsScopeBar impORTANT系统throw异常,如果将属性设置为yes表示在搜索栏,该错误信息显示在导航栏上。

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _searchDsplyCntrl.searchResultsTableView)
    {
        return [_filteredResults count];
    }
    else
    {
        return [_unfilteredResults count];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if ( cell == nil ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Create a new Candy Object
    ResultObj *result = nil;
    // Check to see whether the normal table or search results table is being displayed and set the Candy object from the appropriate array
    if (tableView == _searchDsplyCntrl.searchResultsTableView)
    {
        result = [_filteredResults objectAtIndex:[indexPath row]];
    }
    else
    {
        result = [_unfilteredResults objectAtIndex:[indexPath row]];
    }
    // Configure the cell
    [[cell textLabel] setText:[result name]];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    return cell;
}
#pragma mark Content Filtering
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    // Update the filtered array based on the search text and scope.
    // Remove all objects from the filtered search array
    [_filteredResults removeAllObjects];
    // Filter the array using NSPredicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
    NSArray *tempArray = [_unfilteredResults filteredArrayUsingPredicate:predicate];
    /*
    if(![scope isEqualToString:@"All"]) {
        // Further filter the array with the scope
        NSPredicate *scopePredicate = [NSPredicate predicateWithFormat:@"SELF.category contains[c] %@",scope];
        tempArray = [tempArray filteredArrayUsingPredicate:scopePredicate];
    }
    */
    _filteredResults = [NSMutableArray arrayWithArray:tempArray];
}
#pragma mark - UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    // Tells the table data source to reload when text changes
    [self filterContentForSearchText:searchString scope:
     [[_searchDsplyCntrl.searchBar scopeButtonTitles] objectAtIndex:[_searchDsplyCntrl.searchBar selectedScopeButtonIndex]]];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    // Tells the table data source to reload when scope bar selection changes
    [self filterContentForSearchText:[_searchDsplyCntrl.searchBar text] scope:
     [[_searchDsplyCntrl.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}
原作者:
...