2014-09-15 6 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

Try следующий фрагмент кода:

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

Смежные вопросы