更改 ABPeoplePickerNavigationController 内 UISearchBar 的tintColor

发布于 2024-09-17 10:26:55 字数 1047 浏览 16 评论 0原文

我正在尝试在 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];

自定义导航栏的色调颜色,此行有效:

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

宛菡 2024-09-24 10:26:55

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

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

searchDisplayController 两次都是 nil。我最终做的是遍历视图层次结构以找到 UISearchBar 视图。肯定有一个是对的。这是我的最终代码。如果有人有更好的解决方案,我很想听听。

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

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:@"> "];
}
吃颗糖壮壮胆 2024-09-24 10:26:55

我没有更改颜色,而是将图像添加到背景中:

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

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

[mysearchBar setBackgroundImage:[UIImage imageNamed:@"<#Your image name#>"]];
棒棒糖 2024-09-24 10:26:55

您现在可以使用 UISearchBar 外观代理来执行此操作:

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

You can do this using the UISearchBar appearance proxy now:

UISearchBar* addressBookSearchBar = [UISearchBar appearanceWhenContainedIn:[ABPeoplePickerNavigationController class], nil];
addressBookSearchBar.tintColor = [UIColor blackColor];
凝望流年 2024-09-24 10:26:55

对于 iOS7+,最简单的方法是操作 UISearchBar 的外观代理:

[[UISearchBar appearance] setBarTintColor:[UIColor blackColor]];

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

[[UISearchBar appearance] setBarTintColor:[UIColor blackColor]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文