Question

I am tyring to customize the colors a bit in a ABPeoplePickerNavigationController below is my complete 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];

Customizing the NavigationBar tintColor, this line works:

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

But, I'd also like to customize the tintColor of the searchBar:

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

That line does not work. I think I may be referencing the wrong searchBar....can you point me in the right direction?

Was it helpful?

Solution

I just ran into the same problem. Changing the navBar color is easy. Changing the UISearchBar color however not so much. First what I did was to check

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

Both times the searchDisplayController was nil. What I ended up doing was traversing the view hierarchy to find the UISearchBar view. There is certainly one there right. So this is my final code. If anyone has a better solution I would love to hear it.

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:@"> "];
}

OTHER TIPS

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

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

You can do this using the UISearchBar appearance proxy now:

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

For iOS7+ the easiest way is manipulating the appearance proxy of the UISearchBar:

[[UISearchBar appearance] setBarTintColor:[UIColor blackColor]];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top