问题
I have setup a UISearchBarController like so:
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchBar.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.prompt = @"Searching Transactions Since Nov 11 2017";
self.searchController.searchBar.placeholder = @"Search";
This is the output of setting .prompt
:
That is how it looks when the view presents and during scrolling. The prompt
should be showing above the search bar, as seen in the HIG.
Does anyone know why it might be doing this? Is it a bug? Or is the prompt
only meant to show properly when setting a separate search controller?
回答1:
Not sure it is a bug on UISearchBar, but you can add offset:
self.searchController.searchBar.searchFieldBackgroundPositionAdjustment = UIOffsetMake(0, 20);
And in case you have cancel button:
UIBarButtonItem *cancelButton = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
[cancelButton setTitlePositionAdjustment:UIOffsetMake(0, 20) forBarMetrics:UIBarMetricsDefault];
来源:https://stackoverflow.com/questions/47910089/uisearchbar-prompt-not-displaying-properly