Change tintColor UISearchBar inside ABPeoplePickerNavigationController

I use tyring to adjust colors in ABPeoplePickerNavigationController below, this is my full code:

 ABPeoplePickerNavigationController *objPeoplePicker = [[ABPeoplePickerNavigationController alloc] init]; [objPeoplePicker setPeoplePickerDelegate:self]; objPeoplePicker.topViewController.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.294 green:0.278 blue:0.247 alpha:1.0]; objPeoplePicker.topViewController.searchDisplayController.searchBar.tintColor = [UIColor colorWithRed:0.294 green:0.278 blue:0.247 alpha:1.0]; [self presentModalViewController:objPeoplePicker animated:YES]; 

Setting up TintColor NavigationBar, this line works:

 objPeoplePicker.topViewController.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.294 green:0.278 blue:0.247 alpha:1.0]; 

But I would also like to configure tintColor for searchBar:

 objPeoplePicker.topViewController.searchDisplayController.searchBar.tintColor = [UIColor colorWithRed:0.294 green:0.278 blue:0.247 alpha:1.0]; 

This line does not work. I think that I can refer to the wrong search Bar .... Can you point me in the right direction?

+4
source share
4 answers

I ran into the same problem. Changing the color of navBar is very simple. The color change of the UISearchBar, however, is not much. I checked first

 if( picker.searchDisplayController == nil ) NSLog(@"searchDisplayController is nil"); if( picker.topViewController.searchDisplayController == nil ) NSLog(@"topViewController.searchDisplayController is nil"); 

Both times the searchDisplayController was zero. What I ended up with is moving the view hierarchy to find the UISearchBar view. There, of course, is right. So this is my last code. If anyone has a better solution, I would love to hear that.

 static BOOL foundSearchBar = NO; - (void)findSearchBar:(UIView*)parent mark:(NSString*)mark { for( UIView* v in [parent subviews] ) { if( foundSearchBar ) return; NSLog(@"%@%@",mark,NSStringFromClass([v class])); if( [v isKindOfClass:[UISearchBar class]] ) { [(UISearchBar*)v setTintColor:[UIColor blackColor]]; foundSearchBar = YES; break; } [self findSearchBar:v mark:[mark stringByAppendingString:@"> "]]; } } - (void)pickPerson:(BOOL)animated { foundSearchBar = NO; ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; [[picker navigationBar] setTintColor:[UIColor blackColor]]; picker.peoplePickerDelegate = self; picker.displayedProperties = [NSArray arrayWithObjects: [NSNumber numberWithInt:kABPersonEmailProperty], nil]; [self presentModalViewController:picker animated:animated]; [picker release]; [self findSearchBar:[picker view] mark:@"> "]; } 
+3
source

Instead of changing the color, I add the image to the background:

 [mysearchBar setBackgroundImage:[UIImage imageNamed:@"<#Your image name#>"]]; 
+1
source

Now you can do this using the UISearchBar proxy:

 UISearchBar* addressBookSearchBar = [UISearchBar appearanceWhenContainedIn:[ABPeoplePickerNavigationController class], nil]; addressBookSearchBar.tintColor = [UIColor blackColor]; 
+1
source

For iOS7 +, the easiest way is to manage the UISearchBar lookup proxy:

 [[UISearchBar appearance] setBarTintColor:[UIColor blackColor]]; 
+1
source

Source: https://habr.com/ru/post/927231/


All Articles