0

I need the UISearchBar to always stay inside the NavigationBar. But so far self.searchDisplayController.displaysSearchBarInNavigationBar = YES; is not doing it. When I first start the app the searchBar is inside the NavigationBar. But as soon as I click on it, it smacks itself right in the middle of my scene/screen and never leaves.

This question is a follow up to How do I add storyboard-based Header and CustomTableCell to a “Search Bar and Search Display Controller”

My .h file is

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate>

@end

and my .m file is

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)NSMutableArray *data;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
    self.data=[[NSMutableArray alloc]init];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [_data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CellIdentifier";

    // Dequeue or create a cell of the appropriate type.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    // Configure the cell.
    cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"Row %d: %@", indexPath.row, [_data objectAtIndex:indexPath.row]];
    return cell;
}

#pragma mark - delegate

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(mockSearch:) userInfo:searchString repeats:NO];
    return NO;
}

- (void)mockSearch:(NSTimer*)timer
{
    [_data removeAllObjects];
    int count = 1 + random() % 20;
    for (int i = 0; i < count; i++) {
        [_data addObject:timer.userInfo];
    }
    [self.searchDisplayController.searchResultsTableView reloadData];
}

@end

And that’s the entire program.

Community
  • 1
  • 1
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199
  • That should work, so I think you need to include some code so we can see how you're setting things up. – rdelmar Jul 21 '14 at 20:38
  • @rdelmar the code is in the SO link I provide. But sure I can copy and paste it here as well. – Katedral Pillon Jul 21 '14 at 20:45
  • How did you set things up in IB? I tried your code and it worked fine, so we must have set things up differently in the storyboard. – rdelmar Jul 21 '14 at 20:56
  • @rdelmar This is a tough one. I am looking, and I can't find anything. My one and only view is a `Search Bar and Search Display Controller`. For full disclosure it goes like this: `TabBarController => NavigationController => ViewController => "Search Bar and Search Display Controller"` – Katedral Pillon Jul 21 '14 at 21:26
  • @rdelmar is there anything in the `Attributes inspector` that might cause this? I can't find any so far. – Katedral Pillon Jul 21 '14 at 21:31
  • A strange thing about this is that although the searchBar is showing at the center of my screen, it still thinks it's in the navigationBar? If I click on it at the center it does not respond; but if I click on the navigationBar, then it response: cursor and keyboard. So there is a mismatch between functionality and view. – Katedral Pillon Jul 21 '14 at 21:50

0 Answers0