UISearchDisplayController gap between overlay and

2019-05-05 15:52发布

问题:

In my app I'm trying to add search functionality. I have table view controller and custom top bar view where I show UISearchBar. The problem is that overlay view is always a bit under the top bar and it adds gap between them:

In my table view controller .m file's viewDidLoad:

[super viewDidLoad];

LSDropdownViewController *menuCtrl = (LSDropdownViewController *)[self parentViewController];

menuCtrl.topSearchBar.delegate = self;
[menuCtrl.topSearchBar setBackgroundColor:[UIColor clearColor]];
[menuCtrl.topSearchBar setBackgroundImage:[UIImage imageWithGradientColors]];
[menuCtrl.topSearchBar setTintColor:[UIColor whiteColor]];

self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:menuCtrl.topSearchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;

any ideas?

回答1:

It's due to iOS7+. So, to get rid of it, you have to set search bar frame manually by offsetting y-value 20 pixels i.e. searchBar.frame = CGRectMake(0,20,width,height).