![](/img/trans.png)
[英]how to change background color of the navigation bar and navigation text color
[英]Cannot change text and background color of searchbar in navigation bar
我已在導航欄中添加了搜索欄,並希望更改文本和背景的顏色。 我正在使用下面的代碼,並基於對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;
以下代碼幫助我解決了該問題。 我已閱讀過一些設置私有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;
}
}
}
嘗試以下代碼:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.