1

使用以下代码,范围栏是不可见的(按钮工作但不可见 - 只是黑色空间)

UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44 + (_showScope?40:0));];
searchBar.barStyle = UIBarStyleBlack;

searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"string1", @"string2", nil];
searchBar.showsScopeBar = YES;
searchBar.delegate = self;

self.tableView.tableHeaderView = searchBar;

在 iOS6 中运行良好。有任何想法吗?

4

3 回答 3

2

这似乎是一种解决方法:

UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
searchBar.barStyle = UIBarStyleBlack;

searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"string1", @"string2", nil];
searchBar.showsScopeBar = YES;
searchBar.delegate = self;

self.tableView.tableHeaderView = searchBar;

searchBar.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 44 + (_showScope?40:0));

好的!

于 2013-09-24T14:41:43.923 回答
1

感谢 Nick H247 让我走上正轨。但是,他的解决方法给我带来了另一个问题,搜索结果列表显示在搜索栏后面,因此我无法从列表的开头选择搜索结果。我通过在将 UISearchBar 分配给 tableHeaderView之前设置框架来解决此问题。

UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
searchBar.barStyle = UIBarStyleBlack;

searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"string1", @"string2", nil];
searchBar.showsScopeBar = YES;
searchBar.delegate = self;

searchBar.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 44 + (_showScope?40:0));

self.tableView.tableHeaderView = searchBar;
于 2013-11-14T19:10:01.597 回答
0

很简单,调用 searchBar.sizeToFit;

于 2015-05-05T22:04:18.937 回答