Chris
Chris

Reputation: 1034

How do you programmatically set UISearchDisplayBar inside the NavigationBar for UITableViewController

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.sstatic.net/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!

Upvotes: 0

Views: 1326

Answers (2)

Akash
Akash

Reputation: 501

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

Upvotes: 0

Tarek Hallak
Tarek Hallak

Reputation: 18470

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];

}

Upvotes: 1

Related Questions