代码之家  ›  专栏  ›  技术社区  ›  Jesse Bunch

在abpeoplePickerNavigationController内更改uiSearchBar的淡色

  •  4
  • Jesse Bunch  · 技术社区  · 14 年前

    我正努力在 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];
    

    那条线坏了。我想我可能引用了错误的搜索栏……你能给我指出正确的方向吗?

    4 回复  |  直到 10 年前
        1
  •  3
  •   ribeto    13 年前

    我也遇到了同样的问题。更改导航栏颜色很容易。但是,更改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:@"> "];
    }
    
        2
  •  1
  •   Pach    11 年前

    我不更改颜色,而是在背景中添加图像:

    [mysearchBar setBackgroundImage:[UIImage imageNamed:@"<#Your image name#>"]];
    
        3
  •  1
  •   lintmachine    11 年前

    现在可以使用uisearchbar外观代理进行此操作:

    UISearchBar* addressBookSearchBar = [UISearchBar appearanceWhenContainedIn:[ABPeoplePickerNavigationController class], nil];
    addressBookSearchBar.tintColor = [UIColor blackColor];
    
        4
  •  1
  •   capikaw    10 年前

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

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