好的,所以我想在左侧有一个带有UIButton的UISearchbar,并且在搜索时我发现最好的方法是将这两个项目添加到UIToolbar,然后将其添加到标题视图tableview。所以我得到了那个部分,没有问题,但是当我开始打字时,我也想让工具栏上显示取消按钮,所以我再次搜索并找到它以便让它工作UISearchbar需要嵌套在UIView中,并且我的问题出现了。我不知道是否由于我添加商品的顺序或由于我添加商品的顺序而是由于视图的背景颜色或其他东西干扰了工具栏的背景颜色。如果您查看下面的屏幕截图,UIToolBar的背景颜色(仅使用darkGrayColor)会正确显示,但SearchBar的颜色会更深。我尝试将握持视图的背景设置为clearColor,甚至尝试将SearchBar的背景颜色设置为清除,但到目前为止我似乎无法让UISearchBar与之匹配我有UIBarButton。以下是我设置SearchBar / ToolBar
的代码
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 0) {
//Create the SearchBar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 280, 44)];
searchBar.placeholder = @"Search for Client";
searchBar.delegate = self;
searchBar.tag = kSearchBarTag;
//Create view to wrap search bar in
UIView *barWrapper = [UIView new];
barWrapper.frame = searchBar.bounds;
barWrapper.backgroundColor = [UIColor clearColor];
[barWrapper addSubview:searchBar];
UIBarButtonItem *searchBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
//Create the Sort Button
UIBarButtonItem *sortButton = [UIBarButtonItem new];
[sortButton setImage:[UIImage imageNamed:iconSort]];
[sortButton setTarget:self];
[sortButton setAction:@selector(SortButtonPressed:)];
//Create the ToolBar
UIToolbar *searchToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,0,tableView.bounds.size.width,44)];
searchToolbar.backgroundColor = [UIColor darkGrayColor];
[searchToolbar setItems:[NSArray arrayWithObjects:sortButton,searchBarButtonItem, nil] animated:YES];
return searchToolbar;
}
return nil;
}
#pragma mark - SearchBar Delegate
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
searchBar.showsCancelButton = YES;
return TRUE;
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.showsCancelButton = NO;
[searchBar resignFirstResponder];
}
答案 0 :(得分:1)
将搜索样式设置为最小,灰色背景将消失。
界面生成器
打开属性检查器并选择最小
Objective-C代码
searchBar.barStyle = UISearchBarStyleMinimal;