4

我有一個UISearchBar作爲我的表頭添加下面的代碼。是否可以更改UISearchDisplayController搜索欄的邊框顏色?

searchBar = [[UISearchBar alloc] initWithFrame:self.tableView.bounds]; 
searchBar.tintColor = [UIColor colorWithWhite:185.0/255 alpha:1.0]; 
[searchBar sizeToFit]; 
self.tableView.tableHeaderView = searchBar; 

然後我把我的UISearchDisplayController瞭如下。

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; 
[searchDisplayController setDelegate:self]; 
[searchDisplayController setSearchResultsDataSource:self]; 

的一切功能,因爲我想除了UISearchDisplayController已增加的搜索欄上方藍色(ISH)邊界 - 這條不承認tintColor我已經在搜索欄上設置。

是否可以更改此欄的顏色?這顯然不是絕對的關鍵,但是如果它保持藍色的話,那麼這條線將會讓我永遠陷入困境!

zoomed in on UISearchBar http://nikonizer.yfrog.com/Himg256/scaled.php?tn=0&server=256&filename=4y9.png&xsize=640&ysize=640

回答

3

邊框似乎並不與色調的顏色變化非常好,所以我的設計師堅持要我們改變它。就像在搜索欄的底部添加1像素視圖一樣簡單:

現在在viewDidLoad中,我創建一個1px視圖並將其放置在搜索欄的最底部。現在

#define SEARCHBAR_BORDER_TAG 1337 
- (void) viewDidLoad{ 
    // Set a custom border on the bottom of the search bar, so it's not so harsh 
    UISearchBar *searchBar = self.searchDisplayController.searchBar; 
    UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0,searchBar.frame.size.height-1,searchBar.frame.size.width, 1)]; 
    [bottomBorder setBackgroundColor:[UIColor colorWithWhite:200.0f/255.f alpha:1.0f]]; 
    [bottomBorder setOpaque:YES]; 
    [bottomBorder setTag:SEARCHBAR_BORDER_TAG]; 
    [searchBar addSubview:bottomBorder]; 
    [bottomBorder release]; 
} 

,我也過渡色調顏色回默認,當用戶實際搜索,因爲着色搜索欄還色調取消按鈕顏色難看的顏色。如果你做同樣的事情,下面的代碼會隱藏/顯示你的邊界每個狀態:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{ 
    [controller.searchBar setTintColor:nil]; 

    // Hide our custom border 
    [[controller.searchBar viewWithTag:SEARCHBAR_BORDER_TAG] setHidden:YES]; 
} 

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller{ 
    [controller.searchBar setTintColor:[UIColor colorWithRed:238.0f/255.0f green:245.0f/255.0f blue:248.0f/255.0f alpha:1.0f]]; 
    //Show our custom border again 
    [[controller.searchBar viewWithTag:SEARCHBAR_BORDER_TAG] setHidden:NO]; 
} 
+0

尼斯技術,但不幸的是,它是頂部的邊界,導致我的問題,這實際上超出了UISearchBar的界限,不能用這樣的子視圖覆蓋。 – prendio2 2011-05-21 12:56:48

0

試試這個:

searchBar.clipsToBounds = YES; 

原來的問題link

1

searchBar.searchBarStyle = UISearchBarStyleMinimal;