3
votes

In my split view application it is not possible to add search bar to the rootView of the split view

So i added search bar dynamically at the tableHeaderView of the ui table view as folows

searchBar = [[UISearchBar alloc] init];
      searchBar.frame=CGRectMake(0, self.tableView.frame.origin.y, self.tableView.frame.size.width, 44);
      [searchBar sizeToFit];
      self.tableView.tableHeaderView = searchBar;

enter image description here

When Scroll down: iThe tableHeaderView also scrolls down so search bar also scrolls

enter image description here

When Scroll top: The tableHeaderView also scrolls top so search bar also scrolls

enter image description here

I implemented code as follows to resolve this issue this helps only when scrolls down but when we scrolls the table view to topside it again move with the table view

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
      CGRect rect = self.tableView.tableHeaderView.frame;
      rect.origin.y = MIN(0, self.tableView.contentOffset.y);
      self.tableView.tableHeaderView.frame = rect;
}

I need to stick the tableHeaderView/ Search bar at the top of the view always

How to do this

3
did you find out the solution>Nam Vu
i spent much time over this task but still not getting any solution....if you solve above problem then please let me know...Bandish Dave

3 Answers

0
votes

You can add tabBar separate with tableView

mySearchBar = [[UISearchBar alloc] init];
[mySearchBar setHidden:NO];
mySearchBar.placeholder = @"Search item here";
mySearchBar.tintColor = [UIColor darkGrayColor];
mySearchBar.frame = CGRectMake(0, 0, 320, 44);
mySearchBar.delegate = self;
[mySearchBar sizeToFit];
[mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];

[self.view addSubview:mySearchBar];  

And tableView

UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, 320, 436)];
[self.view addSubview:tableView]; 

If You want to add in xib then

enter image description here

0
votes

Put your searchBar in separate view and put that view above the table view. That means it stays constant.

-2
votes

I'm sure this has been answered before, but assuming you're using UITableViewController, you can make the view property be anything you want. So one approach would be to set up a container view with your search bar at the top and the table below it and make view be this container. By default, tableView returns view, so another detail you'd need to take care of is overriding the tableView property to return the actual table view (that you've stored in an ivar). The code might look something like this:

@synthesize tableView = _tableView;

- (void)loadView
{
    [super loadView];

    _tableView = [super tableView];

    // Container for both the table view and search bar
    UIView *container = [[UIView alloc] initWithFrame:self.tableView.frame];

    // Search bar
    UIView *searchBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];

    // Reposition the table view below the search bar
    CGRect tableViewFrame = container.bounds;
    tableViewFrame.size.height = tableViewFrame.size.height - searchBar.frame.size.height;
    tableViewFrame.origin.y = searchBar.frame.size.height + 1;
    self.tableView.frame = tableViewFrame;

    // Reorganize the view heirarchy
    [self.tableView.superview addSubview:container];
    [container addSubview:self.tableView];
    [container addSubview:searchBar];
    self.view = container;
}