0
votes

I've got a TableView and a Search Bar set up and both work fine so far. The problem is that the UISearchBar overlays the first item of my TableView.

I cannot use XIB or storyboards in this project.

The definition and instantiation of the TableView is in a galaxy in a class (imported by a class I import...) far, far away -- not that easy to access.

I move (instantiate) the SearchBar on the screen with:

self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:self.searchBar];
self.searchBar.delegate = self;

I tried moving it to 0,-44 but then the screen does not stay at the SearchBar, it moves back down.

If I try to move the UITableView with:

self.tableView.frame = CGRectMake(0, 0, 320, 500);

it moves the SearchBar as well.

So let's say if I set the origin of the search bar at 0,0 and the origin of the table view at 0,44 I get a screen with (objects in this order):

  • Grey Space with the height of a search bar/table view cell
  • Search bar (still overlaying the first cell of my table view
  • other cells

How can I solve this problem? I think the easiest way would be to add an empty object to my data array so the search bar overlays nothing of importance, but that would not be the nicest way of solving it.

Could anyone tell me how I can add/insert an object to an array and then copy another array into it, beginning with index 1, so my empty object stays in there?

1

1 Answers

0
votes

You can use contentInset property on your tableView. Just set the top inset to height of search bar.

self.tableView.contentInset = UIEdgeInsetsMake(self.searchBar.frame.size.height, 0, 0, 0);

This should solve the issue.