将SearchBar添加到NavigationBar Objective-C iOS 9

时间:2015-10-17 10:21:15

标签: ios objective-c uinavigationcontroller uisearchbar

我认为标题非常自我解释。我有一个嵌入在导航控制器中的tableView,并希望在导航栏中添加一个搜索栏。我认为iOS 9中有关于搜索栏控制器的东西发生了变化,所以请记住这必须适用于iOS 9.这就是我所拥有的。毫无疑问,这是错误的。

 rowChart = dc.rowChart('#rowChart');
 var heightRowChart=0;
 heightRowChart = rowChartDimensionGroup.all().length;

 tmpRowChart /* dc.rowChart('#day-of-week-chart', 'chartGroup') */
 .width(300)
 .height(heightRowChart*30)
 ...

3 个答案:

答案 0 :(得分:25)

通过故事板添加它。在视图控制器场景中添加搜索栏,例如退出并添加第一响应者,然后将导航项的titleView提供给搜索栏。

Check this image for reference

答案 1 :(得分:6)

//This method has deprecated in ios 8. So, before ios 8 you can use this.

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[searchBar sizeToFit];

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

//对于ios8 / ios9,请使用以下代码

UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:self];
// Use the current view controller to update the search results.
searchController.searchResultsUpdater = self;
// Install the search bar as the table header.
self.navigationItem.titleView = searchController.searchBar;
// It is usually good to set the presentation context.
self.definesPresentationContext = YES;

答案 2 :(得分:0)

如果您使用的是UISearchDisplayController,这应该可以。

Apple Docs

searchDisplayController.displaysSearchBarInNavigationBar = true