如何为UITableView tableHeaderView设置宽度比UITableView宽度宽?

时间:2012-07-04 07:14:28

标签: ios uitableview cgrect

我在UITableView方法中创建了viewDidLoad,如下所示:

_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];

我想为将拥有此框架的UISearchBar UITableView添加tableHeaderView

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

在为UISearchBar设置UITableView tableHeaderView后,它的帧被拉伸为UITableView的宽度。我可以以某种方式改变UISearchBar的框架吗?

2 个答案:

答案 0 :(得分:0)

在我看来,将UISearchBar置于宽度小于实际UITableView的{​​{1}}内并没有多大意义。我认为这将是拉伸它的默认行为(在这种情况下缩小它)。你可以看看这个question/answe r来获得一些想法(主要是因为打印屏幕和一些答案)。我建议的是UISearchBar以上的UISearchBar,就是这样......

答案 1 :(得分:0)

包含屏幕截图的完整代码

#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