本文介绍了在ABPeoplePickerNavigationController中更改UISearchBar的tintColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

自定义NavigationBar tintColor,此行有效:

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

但是,我还想自定义searchBar的tintColor:

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

该行不起作用.我想我可能引用了错误的searchBar....您能指出我正确的方向吗?

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

推荐答案

我只是遇到了同样的问题.更改navBar颜色很容易.但是,更改UISearchBar的颜色不是很多.首先,我要做的是检查

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");

两次searchDisplayController均为零.我最终要做的是遍历视图层次结构以找到UISearchBar视图.那里肯定有一个.这是我的最终代码.如果有人有更好的解决方案,我很想听听.

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

这篇关于在ABPeoplePickerNavigationController中更改UISearchBar的tintColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 15:46