2014-09-15 4 views
1

검색 모음에 검색 모음을 추가했으며 텍스트와 배경색을 변경하려고합니다. 나는 아래 코드를 사용하고 있으며 SO appearanceWhenContainedIn에 대한 다른 답변을 바탕으로 작동하지만 아무것도하지 않습니다. 여기서 뭐가 잘못 됐어? 코드 조각 다음탐색 모음에서 검색 모음의 텍스트와 배경색을 변경할 수 없습니다.

UISearchBar * theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,40)]; // frame has no effect. 

    theSearchBar.delegate = self; 
    if (!searchBarPlaceHolder) { 
     searchBarPlaceHolder = @"What are you looking for?"; 
    } 
    theSearchBar.placeholder = searchBarPlaceHolder; 
    theSearchBar.showsCancelButton = NO; 

    [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]]; 

    UISearchDisplayController *searchCon = [[UISearchDisplayController alloc] 
      initWithSearchBar:theSearchBar 
      contentsController:self ]; 

    [searchCon setActive:NO animated:YES]; 
    self.searchDisplayController = searchCon; 
    self.searchDisplayController.delegate = self; 
    self.searchDisplayController.searchResultsDataSource = self; 
    self.searchDisplayController.searchResultsDelegate = self; 
    self.searchDisplayController.displaysSearchBarInNavigationBar=YES; 

답변

1

다음 코드는 제가 문제를 해결할 수있었습니다. 개인 API를 설정하는 메소드는 apple에서 거부 될 수 있으며 루트는 subView입니다. 아래 참조 :

if(!itemSearchBar) 
    { 
     itemSearchBar = [[UISearchBar alloc] init]; 
     [itemSearchBar setFrame:CGRectMake(0, 0, self.view.frame.size.width, 55)]; 
     itemSearchBar.placeholder = @"What are you looking for?"; 
     itemSearchBar.delegate = self; 


     UITextField* sbTextField; 

     for (UIView *subView in self.itemSearchBar.subviews){ 
      for (UIView *ndLeveSubView in subView.subviews){ 

       if ([ndLeveSubView isKindOfClass:[UITextField class]]) 
       { 

        sbTextField = (UITextField *)ndLeveSubView; 
        //changes typing text color 
        sbTextField.textColor = [UIColor redColor]; 
        //changes background color 
        sbTextField.backgroundColor = [UIColor blueColor]; 
        //changes placeholder color 
          UIColor *color = [UIColor redColor]; 
          sbTextField.attributedPlaceholder = 
          [[NSAttributedString alloc] 
          initWithString:@"What are you looking for?" 
          attributes:@{NSForegroundColorAttributeName:color}]; 

        break; 
       } 

      } 

     } 
0

봅니다 :

if(!searchBar) 
{ 
    searchBar = [[UISearchBar alloc] init]; 
    //  searchBar.backgroundColor = [UIColor purpleColor]; 
    //  searchBar.tintColor = [UIColor purpleColor]; 
    searchBar.barTintColor = [UIColor purpleColor]; 
    [searchBar setFrame:CGRectMake(0, 0, self.view.frame.size.width, 55)]; 
    searchBar.placeholder = @"Search here"; 
    searchBar.delegate = self; 
    UITextField *txfSearchField = [searchBar valueForKey:@"_searchField"]; 
    txfSearchField.backgroundColor = [UIColor purpleColor]; 
    //[txfSearchField setLeftView:UITextFieldViewModeNever]; 
    [txfSearchField setBorderStyle:UITextBorderStyleRoundedRect]; 
    txfSearchField.layer.borderWidth = 0.9f; 
    txfSearchField.layer.cornerRadius = 5.0f; 
    txfSearchField.layer.borderColor = [UIColor cyanColor].CGColor; 
    //txfSearchField.background = [UIImage imageNamed:@"Navgation_bar.png"]; 
    //[searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"Navgation_bar.png"] forState:UIControlStateNormal]; 
    //[searchBar setBackgroundImage:[UIImage imageNamed:@"Navgation_bar.png"]]; 
    //searchBar.barStyle = UISearchBarStyleProminent; 
} 
+0

이 정보는 도움이됩니다. 배경색은 변경할 수 있지만 텍스트 색상은 변경할 수 없습니다. 나는 tintColor와 setTintColor를 시도했다. 어떤 제안? – Ryasoh

관련 문제