gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:37:41 24 4
gpt4 key购买 nike

我在我的导航栏中添加了一个搜索栏,并且想要更改文本和背景的颜色。我正在使用下面的代码并基于 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;

最佳答案

以下代码帮助我解决了这个问题。我已经阅读了设置私有(private) API 的方法可能会被苹果拒绝,并且要走的路线是 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;
}

}

}

关于ios - 无法更改导航栏中搜索栏的文本和背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25840410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com