在导航栏iOS上添加UISearchController

时间:2016-05-25 16:33:19

标签: ios objective-c uinavigationcontroller uinavigationbar uisearchbar

我无法在iOS 9导航栏上添加UISearch控制器。互联网上有很多,但没有一个可以工作..

代码是..

- (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{}

1 个答案:

答案 0 :(得分:1)

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

[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;

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