好吧,所以我想在左邊有一個UIButton的UISearchbar,並且從四處搜索我發現這樣做的最好方法是將兩個項目添加到UIToolbar,然後將其添加到tableview的標題視圖。所以我把那部分放下了,沒有任何問題,但是我也希望在工具欄上顯示取消按鈕,當用戶開始輸入時,我再次搜索並發現爲了讓它工作, UISearchbar需要嵌套在UIView中,這就是我的問題發生的地方。我不知道這是由於我添加項目的順序,還是由於添加項目的順序,但視圖或其他內容的背景顏色干擾了背景顏色工具欄。如果你看下面的屏幕截圖,UIToolBar的背景顏色(只是使用darkGrayColor)正確顯示,但是SearchBar的顏色較暗。我已經嘗試將持有視圖的背景設置爲clearColor,甚至嘗試將SearchBar的背景色設置爲清除,但目前爲止,我似乎無法使UISearchBar與我擁有的UIBarButton相匹配。下面是我對設置搜索欄/工具欄UISearchBar裏面的UIView裏面的UIToolbar背景顏色問題
-(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];
}