UISearchController and it's use of UISearchBar on iOS 8 has several quirks. This particular gist is to help get around the issue where showsCancelButton = YES/NO is not listened to on UISearchBar inside of UISearchController.
Created
September 9, 2015 04:51
-
-
Save vinnybad/b455e2b6d49f045a73b1 to your computer and use it in GitHub Desktop.
UISearchController in iOS 8 - Working around showing multiple 'Cancel' button quirks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BJMSearchBar.h | |
// | |
// Created by Vinayak Ram on 9/8/15. | |
// | |
#import <UIKit/UIKit.h> | |
@interface BJMSearchBar : UISearchBar | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BJMSearchBar.m | |
// | |
// Created by Vinayak Ram on 9/8/15. | |
// | |
#import "BJMSearchBar.h" | |
@implementation BJMSearchBar | |
-(void)layoutSubviews{ | |
[super layoutSubviews]; | |
[self setShowsCancelButton:NO animated:NO]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BJMSearchController.h | |
// | |
// Created by Vinayak Ram on 9/8/15. | |
// | |
#import <UIKit/UIKit.h> | |
@interface BJMSearchController : UISearchController | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BJMSearchController.m | |
// | |
// Created by Vinayak Ram on 9/8/15. | |
// | |
#import "BJMSearchController.h" | |
#import "BJMSearchBar.h" | |
@interface BJMSearchController ()<UISearchBarDelegate> { | |
UISearchBar *_searchBar; | |
} | |
@end | |
@implementation BJMSearchController | |
-(UISearchBar *)searchBar { | |
if( !_searchBar ) { | |
UISearchBar *searchBar = [super searchBar]; | |
_searchBar = [[BJMSearchBar alloc] initWithFrame:searchBar.frame]; | |
_searchBar.delegate = self; | |
} | |
return _searchBar; | |
} | |
#pragma mark - UISearchBarDelegate | |
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { | |
[self.searchResultsUpdater updateSearchResultsForSearchController:self]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// inherits from UIViewController | |
// contains a UITableView outlet -> self.tableView | |
@implementation BJMViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellReuseIdentifier]; | |
self.title = NSLocalizedString(@"Search Bhajans", @"Search screen nav bar title"); | |
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelTapped:)]; | |
self.searchResultsController.searchBar.showsCancelButton = NO; | |
[self.searchResultsController.searchBar sizeToFit]; | |
UISearchBar *searchBar = self.searchResultsController.searchBar; | |
self.navigationItem.titleView = searchBar; | |
self.searchResultsController.active = YES; | |
} | |
- (void)cancelTapped:(id)sender { | |
self.searchResultsController.active = NO; | |
[self dismissViewControllerAnimated:YES completion:nil]; | |
} | |
- (UISearchController *)searchResultsController { | |
if( !_searchResultsController ) { | |
_searchResultsController = [[BJMSearchController alloc] initWithSearchResultsController:nil]; | |
_searchResultsController.searchResultsUpdater = self; | |
_searchResultsController.hidesNavigationBarDuringPresentation = false; | |
_searchResultsController.dimsBackgroundDuringPresentation = false; | |
_searchResultsController.delegate = self; | |
} | |
return _searchResultsController; | |
} | |
// uitableview data source and delegate methods here... | |
#pragma mark - UISearchResultsUpdating | |
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController { | |
NSString *query = searchController.searchBar.text; | |
// call your update model / query logic here | |
[self.tableView reloadData]; | |
} | |
#pragma mark - UISearchControllerDelegate | |
-(void)willPresentSearchController:(UISearchController *)searchController { | |
[self.searchResultsController.searchBar becomeFirstResponder]; | |
} | |
-(void)didPresentSearchController:(UISearchController *)searchController { | |
[searchController.searchBar becomeFirstResponder]; | |
} | |
-(void)willDismissSearchController:(UISearchController *)searchController { | |
[self dismissViewControllerAnimated:YES completion:nil]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment