Ios 如何设置比UITableView宽度更宽的UITableView tableHeaderView的宽度?
我在Ios 如何设置比UITableView宽度更宽的UITableView tableHeaderView的宽度?,ios,uitableview,cgrect,Ios,Uitableview,Cgrect,我在viewDidLoad方法中创建了UITableView,如下所示: _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.ta
viewDidLoad
方法中创建了UITableView
,如下所示:
_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];
我想为具有以下框架的UITableView
tableHeaderView
添加UISearchBar
:
CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)
在我为
UITableView tableHeaderView
设置了我的UISearchBar
之后,它的框架会拉伸到UITableView的宽度。我可以改变UISearchBar的框架吗?我认为将UISearchBar
放在宽度小于实际UISearchBar
的UITableView
中没有多大意义。我认为这将是拉伸它(在本例中是收缩它)的默认行为。你们可以看一下这个r来获得一些想法(主要是因为印刷屏幕和一些答案)。我建议的是UITableView
上方的UISearchBar
,就是这样…带屏幕截图的完整代码
#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
在此处设置tableHeaderView的宽度,并在此处添加搜索栏。你能添加那个代码吗?但是当我滚动
UITableView
时,我需要在UINavigationController
下隐藏UISearchBar
。在这个代码中,如果导航栏在那里,它将在导航栏后面隐藏搜索栏