在我的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;
}
但我看不到搜索栏。我只看到了
我错过了什么?
由于
答案 0 :(得分:1)
您可以编程方式添加搜索栏,如下所示:
使 UISearchBar 和 UISearchDisplayController 的属性,即。
@property (nonatomic,strong) UISearchBar *searchBar ; @property (nonatomic,strong) UISearchDisplayController *searchDisplayController;
然后编写以下代码:
- (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 委托
希望此代码适合您。试一试,让我知道您的反馈意见。
答案 1 :(得分:0)
现在我找到了答案,我只是在我身边进行了测试,并且在我的最后工作正常,
// 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]];