2010-09-01 47 views
4

我tyring自定义颜色之下的ABPeoplePickerNavigationController一点是我的完整代码:改变的UISearchBar的tintColor内的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]; 

自定义导航栏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]; 

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

回答

3

我刚碰到同样的问题。更改navBar颜色很容易。不过,更改UISearchBar的颜色并不是那么多。首先我做的是检查

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

这两次searchDisplayController都是零。我最终做的是遍历视图层次结构来查找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:@"> "]; 
} 
+0

谢谢,即使这个线程太老了,以后可能对我有用。 '珍惜时间! – 2011-04-15 16:38:30

+0

之后我注意到的一件事是,如果点击Picker左上角的Groups,然后返回联系人,搜索栏颜色会更改为默认灰色。 – ribeto 2011-04-19 15:18:20

+0

真的很高兴答案:0 – 2012-10-09 09:03:55

1

而是改变颜色的我的图像添加到背景:

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

您现在可以使用的UISearchBar外观代理做到这一点:

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

对于iOS7 +最简单的方法正在操纵UISearchBar的外观代理:

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