0

我在这样UITableViewviewDidLoad方法中创建:

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

我想添加UISearchBarUITableView tableHeaderView具有此框架的内容:

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

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

4

2 回答 2

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

在此处输入图像描述

于 2012-07-04T07:32:32.400 回答
0

在我看来,将一个UISearchBar宽度UITableView小于实际UISearchBar. 我认为这将是拉伸它的默认行为(在这种情况下缩小它)。您可以查看这个问题/答案以获得一些想法(主要是因为打印屏幕和一些答案)。我会建议,将是UISearchBar上面的UITableView,就是这样......

于 2012-07-04T07:24:33.557 回答