无法更改导航栏中搜索栏的文本和背景颜色

时间:2014-09-15 03:24:12

标签: ios text uinavigationbar uisearchbar

我已将搜索栏添加到导航栏,并希望更改文字和背景的颜色。我使用下面的代码并根据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;

2 个答案:

答案 0 :(得分:1)

以下代码帮助我解决了这个问题。我已经读过设置私有API的方法可能会被苹果拒绝,而go to go是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;
                }

            }

        }

答案 1 :(得分: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;
}
相关问题