0

I created UITableView in viewDidLoad method like this:

_tableView = [[UITableView alloc] initWithFrame:CGRectMake(25.0, 0.0, 277.0, 393.0) style:UITableViewStyleGrouped];
mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
[self.tableViewLists setTableHeaderView:mySearchBar];
CGRect newFrame = mySearchBar.frame;
newFrame.size.width = newFrame.size.width + 50.0f;
newFrame.origin.x = newFrame.origin.x - 25.0f;
mySearchBar.frame = newFrame;
[self.tableViewLists setTableHeaderView:mySearchBar];

And I want to add UISearchBar for the UITableView tableHeaderView that will have this frame:

CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)

After I set my UISearchBar for UITableView tableHeaderView, it's frame stretched for the UITableView's width. Can I somehow change frame for UISearchBar?

ZigDanis
  • 154
  • 1
  • 11
  • where you are setting width of tableHeaderView, and where you are adding search bar. Can you add that code? – rishi Jul 04 '12 at 07:17

2 Answers2

0

It doesn't make much sense in my opinion to put an UISearchBar inside an UITableView whose width is smaller than the actual UISearchBar. I think that would be the default behavior to stretch it (in this case shrinked it). You can take a look at this question/answer to get some ideas (mostly because of the print screens and some bits of the answers). What I would advise, would be the UISearchBar above the UITableView, and that's it...

Community
  • 1
  • 1
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
0

full code with screenshot

#import "PhotosViewController.h"

@implementation PhotosViewController

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{

 UITableView *table=[[UITableView alloc] initWithFrame:CGRectMake(25.0,0.0, 277.0, 393.0) style:UITableViewStyleGrouped];

 table.delegate=self;
 table.dataSource=self;

 UISearchBar *searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(10.0f, 0.0f, 257.0f, 44.0f)];

 [table addSubview:searchBar];
 [self.view addSubview:table];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
 return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 return  10;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *cellidentifier=@"cellidentifier";

 UITableViewCell *Cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier];
 if (Cell==nil) {
  Cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
 }

 Cell.textLabel.text=@"hello";
 return Cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
 return 44;
}


@end

enter image description here

Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90