How to prevent the HeaderView from scrolling in the UItable, stick to the top

In my split view application, it is not possible to add a search bar to the root view of a split view

So I added dynamic search bar to tableHeaderView in table ui view as folow

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 scrolling down: iThe TableHeaderView also scrolls down so the search bar also scrolls

enter image description here

When scrolling up: the HeaderView table also scrolls up so that the search bar also scrolls

enter image description here

I implemented the code as follows to solve this problem , but when we scroll through the table view to flip it, we move again with the table view this helps only when scrolls down

- (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 always keep the tableHeaderView / Search bar at the top of the window

How to do it

+3


source to share


3 answers


Place your search in a separate view and place that view above the table view. This means that it remains constant.



0


source


You can add tabBar separately 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 xib then

enter image description here

0


source


I'm sure this has been answered before, but if you use UITableViewController

you can make the property view

what you want. So one way would be to create a container view with your search bar at the top and table below and make view

it a container. tableView

Returns by default view

, so for more information, what you will need to take care of is to override the property tableView

to return the actual table view (which you saved in the 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;
}

      

-2


source







All Articles