4

UISearchBar次のコードをテーブル ヘッダーとして追加しました。

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このバーは、検索バーに設定したものを認識しませんtintColor

このバーの色を変更することはできますか? 明らかに絶対に重要というわけではありませんが、このように青いままであると、その行は永遠に私を悩ませます!

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

4

3 に答える 3

3

境界線は Tint カラーではあまり変化しないように見えるので、私のデザイナーはそれを変更するよう主張しました。検索バーの下部に 1px ビューを追加するだけです。

次に、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];
}
于 2011-05-20T21:18:20.210 に答える
0

これを試して:

searchBar.clipsToBounds = YES;

元の質問リンク

于 2013-04-08T07:36:16.783 に答える