1

好的,所以我想在左侧有一个带有 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];
}
4

1 回答 1

1

将搜索样式设置为最小,灰色背景将消失。

界面生成器

打开属性检查器并选择最小

界面生成器中的最小搜索样式

Objective-C 代码

searchBar.barStyle = UISearchBarStyleMinimal;
于 2014-09-25T09:04:33.067 回答