无法更改搜索栏文本颜色,但我可以更改背景颜色

时间:2014-09-28 19:29:05

标签: ios xcode uitextfield uisearchbar uisearchdisplaycontroller

我在导航栏中嵌入了搜索栏。我能够改变背景颜色,但对于我的生活不能改变文字颜色。我的viewdidload中有以下代码。让文本改变缺少什么?

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 *txfSearchField = [itemSearchBar valueForKey:@"_searchField"];
        txfSearchField.backgroundColor = [UIColor colorWithRed:130/255.0 green:58/255.0 blue:23/255.0 alpha:1.0];


    UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
             initWithSearchBar:itemSearchBar
             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;

我已经读过以下内容应该有效,但不是:

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

我也尝试过:

itemSearchBar.tintColor = [UIColor redColor];
txfSearchField.textColor = [UIColor redColor];

任何建议???

1 个答案:

答案 0 :(得分:0)

我认为您正在查看占位符文本的颜色,而不是在文本字段中键入任何内容。否则,您的代码似乎是正确的。方法

txfSearchField.textColor = [UIColor redColor] 

必须工作。在字段中键入任何内容,您将看到红色。