Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS7:无法将搜索栏添加到导航控制器_Ios_Iphone_Objective C - Fatal编程技术网

iOS7:无法将搜索栏添加到导航控制器

iOS7:无法将搜索栏添加到导航控制器,ios,iphone,objective-c,Ios,Iphone,Objective C,在我的SearchViewController.m中,我添加了 - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBar.barTintColor = [UIColor redColor]; self.searchDisplayController.displaysSearchBarInNavigationBar = YES; [self.searchDi

在我的
SearchViewController.m
中,我添加了

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
    [self.searchDisplayController.searchBar setBarTintColor:[UIColor whiteColor]];


    self.searchResultsView.dataSource = self;
    self.searchResultsView.delegate = self;

    NSLog(@"fetching businesses");
    [self fetchBusinesses];

    // fix UITableViewCell height
    self.tableView.rowHeight = 90;
}
但我看不到搜索栏。我所看到的是

我错过了什么


谢谢

您可以按如下程序添加搜索栏:

UISearchBarUISearchDisplayController的属性设置为

然后编写以下代码:

- (void)viewDidLoad 
{
     searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
        searchBar.delegate =self;
        self.searchBar.showsCancelButton =TRUE;
        [self.searchBar setHidden:NO];
        [self.searchBar becomeFirstResponder];
        [self.searchDisplayController setActive:YES];

        [self.view addSubview:self.searchBar];

        self.searchDisplayController =[[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
        self.searchDisplayController.delegate =self;
        self.searchDisplayController.searchResultsDataSource= self;
        self.searchDisplayController.searchResultsDelegate = self;

}
您还需要为同一个ViewController实现UISearchDisplayDelegate、UISearchBarDelegate委托


希望这段代码对你有用。试试看,让我知道你的反馈。

现在我找到了答案,我只是在我这边测试了一下,在我这边效果很好

 // init search bar
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    searchBar.delegate = self;
    searchBar.showsCancelButton=YES;

    // set up searchDisplayController
    UISearchDisplayController *searchController = [[UISearchDisplayController alloc]
                                                   initWithSearchBar:searchBar contentsController:self];
    searchController.delegate = self;


    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
    [self.searchDisplayController.searchBar setBarTintColor:[UIColor whiteColor]];

他问的是我遗漏了什么,而不是如何实现UISearchBar、displaysSearchBarInNavigationBar方法,该方法仅在ios 7中工作,通过该方法,我们可以在导航栏下设置搜索栏,而无需编写太多代码嘿,它也适用于ios 6。而且它对我来说非常棒。关于遗漏的东西,他可能犯了一些愚蠢的错误,我很乐意帮助他提供逐步解决方案,而不是提供愚蠢的CommentScheck apple开发者文档,你必须在那里找到答案,尽管它是从ios 7开始的。它适用于ios 6、6.1或ios 5。问题是我们需要解决“白日梦者”所面临的问题。我认为无论他写了什么代码,都有缺失的东西,我粘贴的代码片段已经覆盖了这些东西。
 // init search bar
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    searchBar.delegate = self;
    searchBar.showsCancelButton=YES;

    // set up searchDisplayController
    UISearchDisplayController *searchController = [[UISearchDisplayController alloc]
                                                   initWithSearchBar:searchBar contentsController:self];
    searchController.delegate = self;


    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
    [self.searchDisplayController.searchBar setBarTintColor:[UIColor whiteColor]];