无法更改搜索栏文本颜色,但我可以更改背景颜色(Cannot change searchbar text color, but I am able to change background color)

编程入门 行业动态 更新时间:2024-10-27 13:26:21
无法更改搜索栏文本颜色,但我可以更改背景颜色(Cannot change searchbar text color, but I am able to change background color)

我在导航栏中嵌入了一个搜索栏。 我能够改变背景颜色,但对于我的生活不能改变文字颜色。 我的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];

有什么建议么???

I have a searchbar embedded in a navigationbar. I'm able to change background color, but for the life of me cannot change text color. I have the following code in my viewdidload. What is missing to get the text to change?

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;

I've read that the following should work but doesn't:

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

I've also tried:

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

Any suggestions???

最满意答案

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

txfSearchField.textColor = [UIColor redColor]

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

I think you are looking at the color of placeholder text and not typing anything in the textfield. Otherwise, your code seems correct. The method

txfSearchField.textColor = [UIColor redColor]

must work. Type anything in the field and you will see red color.

更多推荐

本文发布于:2023-08-06 13:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448611.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:颜色   但我   文本   背景   change

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!