0

I currently have a table view controller and have placed a UISearchDisplayController on top or above the my table view controller inside the interface builder. Inside the viewDidLoad of my implementation file I go ahead and assign the searchbar to the navigationItem.titleView. The problem that I now have is that there is a bit more space in the first table view cell because it still thinks that the searchdisplaycontroller is there. You can see it here: https://i.stack.imgur.com/PSKLk.jpg

Here is code:

.h file

@interface searchTableViewController : UITableViewController <UISearchBarDelegate>
{
    UISearchBar *searchDrugBar;
}
@property (nonatomic, strong) IBOutlet UISearchBar *searchDrugBar;

.m file

- (void)viewDidLoad
{
    [super viewDidLoad];
    searchDrugBar.placeholder = @"Search for info";

    self.navigationItem.titleView = self.searchDisplayController.searchBar;
    [self.searchDisplayController setActive: YES animated: YES];
    [self.searchDisplayController.searchBar becomeFirstResponder];

}

Any suggestions or ideas? Thanks in advance!

Chris
  • 1,004
  • 2
  • 11
  • 25

2 Answers2

1

Try to create the searchBar programatically, I don't see any added value from creating it in the storyboard unless you have your own reason.

First remove it from the storyboard then:

- (void)viewDidLoad
{
    [super viewDidLoad];

    searchDrugBar = [[UISearchBar alloc] init];
    searchDrugBar.frame = CGRectMake(0, 0, 200,44); // it could be unnecessary
    searchDrugBar.delegate = self;
    searchDrugBar.placeholder = @"Search for info";

    self.navigationItem.titleView = self.searchDisplayController.searchBar;
    [self.searchDisplayController setActive: YES animated: YES];
    [self.searchDisplayController.searchBar becomeFirstResponder];

}
Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
  • I had attempted this before and the reason that I didn't do this is because the Navigation Bar hides and the search bar hides with it and the screen turns grey with a white box atop. Here is what it looks like: http://i.imgur.com/Mk1gIPT.png – Chris Oct 03 '13 at 17:00
0
self.navigationItem.titleView = self.searchBarTop;

=> to pur searchbar left/right :-

UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];

self.navigationItem.rightBarButtonItem = searchBarItem;

- (void)viewDidLoad
{
    ...
    self.searchDisplayController.displaysSearchBarInNavigationBar = true;
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem new];
    ...
}

==> Here Given more Reference It might be Useful I hope it work for You..

How to animate add UISearchBar on top of UINavigationBar

Community
  • 1
  • 1
Akash
  • 461
  • 2
  • 14