UISearchController: show results even when search bar is empty

前端 未结 14 2292
独厮守ぢ
独厮守ぢ 2020-12-23 13:27

As I understand, the default behaviour of UISearchController is:

  1. On tapping search bar, background is dimmed and \'cancel\' button is shown.
14条回答
  •  情深已故
    2020-12-23 13:42

    I spent a lot of time with this, and ultimately the solution I went with is like @malhals's, but the amount of code is significantly reduced by using facebook's KVOController: https://github.com/facebook/KVOController . Another advantage here is that if your searchResultsController is a UINavigationController then you don't need to subclass it just to add @malhal's code.

    // always show searchResultsController, even if text is empty
    [self.KVOController observe:self.searchController.searchResultsController.view keyPath:@"hidden" options:NSKeyValueObservingOptionNew block:^(id observer, UIView* view, NSDictionary *change) {
        if ([change[NSKeyValueChangeNewKey] boolValue] == YES) {
            view.hidden = NO;
        }
    }];
    self.searchController.dimsBackgroundDuringPresentation = NO;
    

提交回复
热议问题