Reputation: 1480
iOS7
.hide
keyboard
properly on search view on iOS8
.@interface MyViewController : UIViewController<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, NSFetchedResultsControllerDelegate, ... >
@implementation MyViewController
@property (nonatomic, retain) UISearchDisplayController *mySearchDisplayController;
- (void)loadView
{
[super loadView];
self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
...
}
-(UITableView*) tableView
{
if (!_tableView){
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,0,0)
style:self.tableViewStyle];
_tableView.translatesAutoresizingMaskIntoConstraints=NO;
_tableView.delegate = self;
_tableView.dataSource=self;
}
return _tableView;
}
-(UISearchBar*) searchBar
{
if(!_searchBar){
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,44,144,0)];
_searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
_searchBar.translatesAutoresizingMaskIntoConstraints=NO;
_searchBar.translucent = NO;
_searchBar.delegate = self;
_searchBar.autoresizingMask = (UIViewAutoresizingFlexibleWidth);
_searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
}
return _searchBar;
}
-(void) initWithTableViewStyle: (int) tableViewStyle
{
//Set the UITableViewStyle
self.tableViewStyle = tableViewStyle;
//Be sure the searchBar won't overlap the status bar
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
//Add the subviews to the mainView
[self.view addSubview:self.searchBar];
[self.view addSubview:self.tableView];
//Create the views dictionaryy
NSDictionary *viewsDictionary = @{@"searchBar":self.searchBar,
@"tableView": self.tableView};
//Create the constraints using the visual language format
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat: @"H:|[searchBar]|"
options:0
metrics:nil
views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat: @"H:|[tableView]|"
options:0
metrics:nil
views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|[searchBar(==44)][tableView]|"
options:0
metrics:nil
views:viewsDictionary]];
}
Now to the interesting part which I believe works differently on iOS7 and iOS8 and causes the issue you can see in the screenshot below:
- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)searchDisplayController: (UISearchDisplayController *)controller
willShowSearchResultsTableView: (UITableView *)searchTableView {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
}
- (void) keyboardWillHide {
UITableView *tableView = self.searchDisplayController.searchResultsTableView;
[tableView setContentInset:UIEdgeInsetsZero];
[tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
}
The selector seemed to do the job in iOS7, but here is the result I get when hiding the keyboard on iOS8:
The bottom rows are cut (the scroller does not enable scrolling down anymore), and the right section indexer is improperly aligned... How can I make this work in iOS8?
Upvotes: 2
Views: 206
Reputation: 874
I do not see code adding layout constraints for the table view. Maybe they are in the -(void) initWithTableViewStyle: (int) tableViewStyle
method? Make sure they are set up properly since it looks like you use the same tableView for displaying all data and the search results.
What's really good about UISearchController is that it gives you the opportunity to provide a separate view controller to manage results display. Which 1st decouples your classes and helps clean up the mess that using a single UITableView or two UITableView's in a single controller creates. So you apparently are in this mess so I suggest diving a bit more into UISearchController and creating a separate class for results display. This way you will not have to worry about contentInsets and so on. Do not use UISearchController the same way as UISearchDisplayController.
Upvotes: 2