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을 설정합니다. 이 바는 내가 검색 창에 설정 한 tintColor 인식하지 않습니다 - 나는 UISearchDisplayController가 검색 창 위에 파란색 (틱) 경계를 추가 한 것을 제외하고 싶습니다

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

모든 기능을 제공합니다.

이 막대의 색상을 변경할 수 있습니까? 그것은 분명히 절대적으로 중요하지는 않지만, 그 라인이 저렇게 남아 있다면 그 라인은 영원히 저를 괴롭힐 것입니다!

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

답변

3

경계는 나의 디자이너는 우리가 그것을 변경하는 것이 주장, 색조 색상으로 잘 변경하지 않는 것 같습니다. 검색 막대의 하단에 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]; 
} 
+0

좋은 기술이지만 유감스럽게도 UISearchBar 바운드 밖에있는 문제는 나를 위 한 경계선이며 이처럼 하위보기로는 다룰 수 없습니다. – prendio2

0

이 시도 :

searchBar.clipsToBounds = YES; 

원래 질문을 link

1

searchBar.searchBarStyle = UISearchBarStyleMinimal;