ios - 在导航栏 iOS 上添加 UISearchController

标签 ios objective-c uinavigationcontroller uinavigationbar uisearchbar

我无法在 iOS 9 的导航栏上添加 UISearch Controller 。互联网上有很多,但没有一个有效。

代码是..

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    searchController.searchResultsUpdater = self;
    searchController.delegate = self;
    searchController.searchBar.delegate = self;
    searchController.hidesNavigationBarDuringPresentation = NO;
    searchController.dimsBackgroundDuringPresentation = YES;

    self.navigationItem.titleView = searchController.searchBar;

    self.definesPresentationContext = YES;

}

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{}

最佳答案

你应该使用。在上面的代码中,您的设置为 nil。所以它不能初始化。

[searchController = [UISearchController alloc] initWithSearchResultController:self]

这里是 iOS 8/9 的代码:

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;

希望它能帮助您解决问题。保持你的热情!祝你好运。

关于ios - 在导航栏 iOS 上添加 UISearchController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37443004/

相关文章:

ios - Swift 无法更新 Parse 对象

objective-c - 我可以在 iOS 上进行这种代码矢量化吗?有哪些替代方案?

objective-c - 从另一个类调用 float 组

ios - UITableView:向上滑动时收缩标签栏和导航栏

显示工具栏时UINavigationController内部的iOS自定义 View 大小

iphone - 将 NSString 传递给另一个 View Controller 会使应用程序崩溃

iOS 11 UICollection Section Header 裁剪滚动指示器

ios - 在 iOS 中共享钥匙串(keychain)项目

objective-c - Objective C block - 语法

objective-c - 使用 UINavigationController View 进行内存管理