我很想在下面的 ABPeoplePickerNavigationController 中自定义颜色,这是我的完整代码:

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];

自定义 NavigationBar tintColor,此行有效:
objPeoplePicker.topViewController.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.294 green:0.278 blue:0.247 alpha:1.0];

但是,我还想自定义 searchBar 的 tintColor:
objPeoplePicker.topViewController.searchDisplayController.searchBar.tintColor = [UIColor colorWithRed:0.294 green:0.278 blue:0.247 alpha:1.0];

那条线不起作用。我想我可能引用了错误的搜索栏....你能指出我正确的方向吗?

最佳答案

我刚刚遇到了同样的问题。更改导航栏颜色很容易。改变 UISearchBar 颜色但不是那么多。首先我做的是检查

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

两次 searchDisplayController 都为零。我最终做的是遍历 View 层次结构以找到 UISearchBar View 。那里肯定有一个。所以这是我的最终代码。如果有人有更好的解决方案,我很乐意听到。
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:@"> "];
}

关于iphone - 在 ABPeoplePickerNavigationController 中更改 UISearchBar 的 tintColor,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3618976/

10-13 04:07