如何更改ABPeoplePickerNavigationController的UINavigationBar的颜色?

时间:2012-10-09 06:43:56

标签: iphone objective-c ios abpeoplepickerview

我正在使用ABAddressBookRef来获取我的电话簿的人员信息。它工作正常,但我想更改UINavigationBar ABPeoplePickerNavigationController的颜色。

这可能与否?如果可能的话,请告诉我该怎么做。

8 个答案:

答案 0 :(得分:7)

设置色彩颜色,就像这样......

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];
[self presentModalViewController:objPeoplePicker animated:YES];

更改UISearchBar颜色

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


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

答案 1 :(得分:2)

您可以使用外观代理来管理ABPeoplePickerNavigationController中导航栏的外观,如下所示:

对于带有白条按钮项目的红色条:

[[UINavigationBar appearanceWhenContainedIn:[ABPeoplePickerNavigationController class], nil] setBarTintColor:[UIColor redColor]];
[[UIBarButtonItem appearanceWhenContainedIn:[ABPeoplePickerNavigationController class], nil] setTintColor:[UIColor whiteColor]];

答案 2 :(得分:1)

您可以尝试使用此代码更改UINavigationBar颜色

    abPeoplePicker.navigationBar.tintColor=[UIColor redColor];

答案 3 :(得分:0)

if ([navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
    UIImage *backGroundImage = [[UIImage imageNamed:kNavigationBarBackGroundImage] stretchableImageWithLeftCapWidth:5 topCapHeight:5];
    [[UINavigationBar appearance] setBackgroundImage:backGroundImage forBarMetrics:UIBarMetricsDefault];
}

这会影响所有导航栏,但您设置背景图片而不是设置背景图像,但设置颜色集只设置栏的颜色

答案 4 :(得分:0)

您可以尝试使用此代码更改 ABPeoplePickerNavigationController 颜色

objPicker.ViewController.navigationController.navigationBar.tintColor = [UIColor redColor];

或者您可以设置 RGB 颜色

  objPicker.ViewController.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.274 green:0.270 blue:0.268 alpha:0.8];

答案 5 :(得分:0)

更改 ABPeoplePickerNavigationController UISearchBar 的颜色 像这样使用....

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

Changing tintColor of UISearchBar inside ABPeoplePickerNavigationController

答案 6 :(得分:0)

试试这个:

static BOOL foundSearchBar = NO;
- (void)findSearchBar:(UIView*)parent mark:(NSString*)mark {

  for( UIView* v in [ 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:@"  "];
}

答案 7 :(得分:0)

picker.topViewController.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};