UISearchBar - addSubview 问题?
UISearchBar - addSubview issue?
我正在尝试在 UITableView
之上添加 UISearchBar
(固定位置!)。
CGRect rect = self.headerView.frame;
CGRect newRect = CGRectMake(0,
rect.origin.y + rect.size.height,
rect.size.width,
CZP_SEARCHBAR_HEIGHT);
UIView *view = [[UIView alloc] initWithFrame:newRect];
view.backgroundColor = [UIColor whiteColor];
结果(我在我想要栏的位置有一个白色矩形):
但是如果我想将子视图添加到我的视图中,搜索栏会出现在表格视图的第一个单元格上(在我的视图下方!)
[view addSubview:searchBar];
这是一种方法。看起来您正在尝试用代码而不是故事板来完成它,所以这是一个代码示例。它看起来也像是在各种弹出窗口中进行操作,我将一个快速项目放在一起作为使用弹出窗口的示例,它看起来与您的不完全一样,但它足够接近您尝试的位置我想去。
首先,这是代码示例,它来自包含 header、搜索栏和表格视图的视图控制器。
- (void)viewDidLoad
{
[super viewDidLoad];
// get the desired size for this popover and setup our header height
CGSize viewSize = self.preferredContentSize; // could also be self.view.bounds.size depending on where you're using it
CGFloat headerHeight = 44.0;
// setup our desired frames
CGRect headerFrame = CGRectMake(0, 0, viewSize.width, headerHeight);
CGRect searchContainerFrame = CGRectMake(0, headerHeight, viewSize.width, headerHeight);
// for this frame I'm simply centering it, there's better ways to do it but this is an example
CGRect searchBarFrame = CGRectMake(5, 5, searchContainerFrame.size.width - 10, searchContainerFrame.size.height - 10);
// set our tableview frame to be positioned below our header and search container frame
CGRect tableviewFrame = CGRectMake(0, headerHeight *2, viewSize.width, viewSize.height - (headerHeight * 2));
// create our header view and set it's background color
UIView *headerView = [[UIView alloc] initWithFrame:headerFrame];
headerView.backgroundColor = [UIColor orangeColor];
// create our container view to hold the search bar (not needed really, but if you want it contained in a view here's how)
UIView *searchContainer = [[UIView alloc] initWithFrame:searchContainerFrame];
searchContainer.backgroundColor = [UIColor greenColor];
// instantiate our search bar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:searchBarFrame];
// add the search bar to the container view
[searchContainer addSubview:searchBar];
// create our tableview and position it below our header and search containers
UITableView *tableview = [[UITableView alloc] initWithFrame:tableviewFrame];
tableview.backgroundColor = [UIColor blueColor];
[self.view addSubview:headerView];
[self.view addSubview:searchContainer];
[self.view addSubview:tableview];
}
该片段为我提供了一个带有橙色 header 的弹出窗口、一个 green/grey 搜索栏和下方的表格视图。
编辑:如果您有兴趣查看我用来将其放在一起的项目文件,您可以下载它 github here
我正在尝试在 UITableView
之上添加 UISearchBar
(固定位置!)。
CGRect rect = self.headerView.frame;
CGRect newRect = CGRectMake(0,
rect.origin.y + rect.size.height,
rect.size.width,
CZP_SEARCHBAR_HEIGHT);
UIView *view = [[UIView alloc] initWithFrame:newRect];
view.backgroundColor = [UIColor whiteColor];
结果(我在我想要栏的位置有一个白色矩形):
但是如果我想将子视图添加到我的视图中,搜索栏会出现在表格视图的第一个单元格上(在我的视图下方!)
[view addSubview:searchBar];
这是一种方法。看起来您正在尝试用代码而不是故事板来完成它,所以这是一个代码示例。它看起来也像是在各种弹出窗口中进行操作,我将一个快速项目放在一起作为使用弹出窗口的示例,它看起来与您的不完全一样,但它足够接近您尝试的位置我想去。
首先,这是代码示例,它来自包含 header、搜索栏和表格视图的视图控制器。
- (void)viewDidLoad
{
[super viewDidLoad];
// get the desired size for this popover and setup our header height
CGSize viewSize = self.preferredContentSize; // could also be self.view.bounds.size depending on where you're using it
CGFloat headerHeight = 44.0;
// setup our desired frames
CGRect headerFrame = CGRectMake(0, 0, viewSize.width, headerHeight);
CGRect searchContainerFrame = CGRectMake(0, headerHeight, viewSize.width, headerHeight);
// for this frame I'm simply centering it, there's better ways to do it but this is an example
CGRect searchBarFrame = CGRectMake(5, 5, searchContainerFrame.size.width - 10, searchContainerFrame.size.height - 10);
// set our tableview frame to be positioned below our header and search container frame
CGRect tableviewFrame = CGRectMake(0, headerHeight *2, viewSize.width, viewSize.height - (headerHeight * 2));
// create our header view and set it's background color
UIView *headerView = [[UIView alloc] initWithFrame:headerFrame];
headerView.backgroundColor = [UIColor orangeColor];
// create our container view to hold the search bar (not needed really, but if you want it contained in a view here's how)
UIView *searchContainer = [[UIView alloc] initWithFrame:searchContainerFrame];
searchContainer.backgroundColor = [UIColor greenColor];
// instantiate our search bar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:searchBarFrame];
// add the search bar to the container view
[searchContainer addSubview:searchBar];
// create our tableview and position it below our header and search containers
UITableView *tableview = [[UITableView alloc] initWithFrame:tableviewFrame];
tableview.backgroundColor = [UIColor blueColor];
[self.view addSubview:headerView];
[self.view addSubview:searchContainer];
[self.view addSubview:tableview];
}
该片段为我提供了一个带有橙色 header 的弹出窗口、一个 green/grey 搜索栏和下方的表格视图。
编辑:如果您有兴趣查看我用来将其放在一起的项目文件,您可以下载它 github here