Rushang Prajapati Rushang Prajapati - 1 year ago 354
Objective-C Question

Custom Searchbar in iOS

Hello i am using

but is there any easy way to create custom
tableview headerview

i already check below link but i am not understand.

Custom UISearchBar with UISearchController

How do I use the UISearchBar and UISearchDisplayController

I have added searchDisplayController : :
enter image description here

And This is my code for searchController,

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = YES;
[self.searchController.searchBar setBarTintColor:[UIColor darkGrayColor]];
self.tableView.tableHeaderView = self.searchController.searchBar;

I have set delegate for
as well,

and implemented their method ,

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
if (_searchController.isActive && _searchController.searchBar.text.length >0){

NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@" contains[cd] %@",

abcd = [sortedEventArray filteredArrayUsingPredicate:resultPredicate];



- (void)updateSearchResultsForSearchController:(UISearchController *)searchController1
NSString *searchString = searchController1.searchBar.text;
[self filterContentForSearchText:searchString scope:@"abc"];
//[self filterContentForSearchText:searchString :@"abc"];
[self.tableView reloadData];

cancel button event

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
abcd=[sortedEventArray mutableCopy];
[_tableView reloadData];



NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest * allMovies = [[NSFetchRequest alloc] init];
[allMovies setEntity:[NSEntityDescription entityForName:@"Data" inManagedObjectContext:context]];
[allMovies setIncludesPropertyValues:NO]; //only fetch the managedObjectID

NSError * error = nil;
NSArray * movies = [context executeFetchRequest:allMovies error:&error];
//error handling goes here
for (NSManagedObject * movie in movies) {
[context deleteObject:movie];
NSError *saveError = nil;
[context save:&saveError];

Still Its not working

Thanks in advance :)

Answer Source

There is everythings is complete but there is two little mistakes by me.

1) delete the search bar from Storyboard.

2) Add [self.searchController.searchBar sizeToFit]; in viewDidLoad

Thanks :))

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download