I am looking for a way how to present a search view with search bar after button is tapped. Am I suppose to create a new UIView with search bar and then after button is tapped segue to that UIView? Or is there a better option?
You can imagine that as searching in Youtube app but you would see search results while typing.
EDIT: So now I managed to present search bar after button is tapped but it is not displaying correctly. Problem is when the search bar is moved lower it is being cut from down:
My code:
implementation ViewController {
NSInteger listNo;
DataClass *dataClass;
UITableViewController *searchResultsController;
}
- (void)viewDidLoad {
[super viewDidLoad];
dataClass = [DataClass getInstance];
self.navigationController.navigationBarHidden = YES;
// Prepare Search Controller and Result Controller
searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y + 5, self.searchController.searchBar.frame.size.width, 44.0);
[self.searchController.searchBar sizeToFit];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = @"Ahoj";
return cell;
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
}
- (void)didDismissSearchController:(UISearchController *)searchController {
}
// Load data to next view
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showTable"]) {
AdditivesTableViewController *atvc = (AdditivesTableViewController *)segue.destinationViewController;
switch (listNo) {
case 0:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.nonDangerousAdditives];
break;
case 1:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.mediumDangerousAdditives];
break;
case 2:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.dangerousAdditives];
break;
case 3:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.highDangerousAdditives];
break;
default:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.allAdditives];
break;
}
}
}
#pragma mark - IBActions
- (IBAction)nonDangList:(id)sender {
listNo = 0;
[self performSegueWithIdentifier:@"showTable" sender:self];
}
- (IBAction)medDangList:(id)sender {
listNo = 1;
[self performSegueWithIdentifier:@"showTable" sender:self];
}
- (IBAction)dangList:(id)sender {
listNo = 2;
[self performSegueWithIdentifier:@"showTable" sender:self];
}
- (IBAction)highDangList:(id)sender {
listNo = 3;
[self performSegueWithIdentifier:@"showTable" sender:self];
}
- (IBAction)searchAdditive:(id)sender {
[self.view addSubview:self.searchController.searchBar];
[self.searchController setActive:YES];
[self.searchController.searchBar becomeFirstResponder];
}
@end