I have a view controller with a search button as a button element of the right navigation bar. When the search button is pressed, I want the UISearchController to be presented. Here is the relevant code:
@interface BaseViewController ()
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) UIBarButtonItem *searchBarButtonItem;
@property (nonatomic, strong) UIBarButtonItem *accountBarButtonItem;
@property (nonatomic, strong) UITableViewController *searchResultsController;
@end
@implementation BaseViewController
- (instancetype)init {
if (self = [super init]) {
UIImage *accountIcon = [UIImage imageNamed:@"account-male-icon@2x.png"];
UIBarButtonItem *accountBarButtonItem = [[UIBarButtonItem alloc] initWithImage:accountIcon style:UIBarButtonItemStylePlain target:self action:@selector(accountButtonPressed:)];
UIBarButtonItem *searchBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchButtonPressed:)];
[self setSearchResultsController:[[UITableViewController alloc] init]];
[self setSearchController:[[UISearchController alloc] initWithSearchResultsController:[self searchResultsController]]];
[[[self searchResultsController] tableView] setDataSource:self];
[[[self searchResultsController] tableView] setDelegate:self];
[[[self searchResultsController] tableView] setTableHeaderView:[[self searchController] searchBar]];
[[self searchController] setSearchResultsUpdater:self];
[[self searchController] setDelegate:self];
[[self searchController] setHidesNavigationBarDuringPresentation:YES];
[[self searchController] setDimsBackgroundDuringPresentation:YES];
[[[self searchController] searchBar] setDelegate:self];
[self setDefinesPresentationContext:YES];
[self setSearchBarButtonItem:searchBarButtonItem];
[self setAccountBarButtonItem:accountBarButtonItem];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[[self navigationItem] setLeftBarButtonItem:[self accountBarButtonItem]];
[[self navigationItem] setRightBarButtonItem:[self searchBarButtonItem]];
}
- (IBAction)searchButtonPressed:(id)sender {
[self presentViewController:[self searchController] animated:YES completion:nil];
}
When the search button is pressed, searchController is displayed. All is well, except for the following two things:
1.) The search bar overrides the status bar
2.) There is a space in the first row of the table view
Below are photos illustrating the problem:


How can I solve the problem in the status bar and remove the space in the first line?
source
share