0

First I build a view add to tableview as tableHeaderView

 UIView *mainHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];

    [self.mainViewTableView setTableHeaderView:mainHeaderView];

I could adjust the Height of mainHeaderView but I cant adjust Y position of this headView.

And I trying to add a sub view:

self.searchForShop = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.mainViewTableView.tableHeaderView.frame.size.width, 44)];
    [self.mainViewTableView addSubview:self.searchForShop];

Run app and found the subview's width is longer than tableHeadView. Do view debug and found the subview is 16 width more than tableHeadView(I run in iphone5s).

How to fix it?

enter image description here

Hasya
  • 9,792
  • 4
  • 31
  • 46
Yan Li
  • 1,699
  • 2
  • 16
  • 25

1 Answers1

0

u can try like this, there might be a problem with setting the width of the tableview, just set the width of search bar to width of tableview

//create a tableview
tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
self.tableView.delegate=self;
self.tableView.dataSource=self;


UISearchBar *searchBar=[[UISearchBar alloc]init];
/* option 1 ->[searchBar sizeToFit]; */
/* option 2 ->*/ searchBar.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 44); //set the width how much table view has
searchBar.delegate=self;
[self.tableView setTableHeaderView:searchBar];
Shankar BS
  • 8,394
  • 6
  • 41
  • 53
  • Thank you for your answer. I tried and find option 1 the searchBar will Short a little than the tableview. option 2 search bar will only 2/3 width of the tableview with – Yan Li Apr 26 '16 at 02:29
  • I am trying to move this my code into viewdidappear and it woks. – Yan Li Apr 26 '16 at 02:38