Skip to content

Instantly share code, notes, and snippets.

@vinnybad
Created September 9, 2015 04:51
Show Gist options
  • Save vinnybad/b455e2b6d49f045a73b1 to your computer and use it in GitHub Desktop.
Save vinnybad/b455e2b6d49f045a73b1 to your computer and use it in GitHub Desktop.
UISearchController in iOS 8 - Working around showing multiple 'Cancel' button quirks

Overview

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.

//
// BJMSearchBar.h
//
// Created by Vinayak Ram on 9/8/15.
//
#import <UIKit/UIKit.h>
@interface BJMSearchBar : UISearchBar
@end
//
// 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
//
// BJMSearchController.h
//
// Created by Vinayak Ram on 9/8/15.
//
#import <UIKit/UIKit.h>
@interface BJMSearchController : UISearchController
@end
//
// 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
// 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