【问题标题】:Add UISearchController on a navigation bar iOS在导航栏 iOS 上添加 UISearchController
【发布时间】:2016-09-23 09:42:08
【问题描述】:

我无法在 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{}

【问题讨论】:

标签: ios objective-c uinavigationcontroller uinavigationbar uisearchbar


【解决方案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;

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

【讨论】:

  • 你试过在 ViewDidLoad 中添加 self.definesPresentationContext = YES 吗?
猜你喜欢
  • 1970-01-01
  • 2017-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多