【问题标题】:Changing tintColor of UISearchBar inside ABPeoplePickerNavigationController在 ABPeoplePickerNavigationController 中更改 UISearchBar 的 tintColor
【发布时间】:2010-09-01 14:13:58
【问题描述】:

我很想在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];

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

【问题讨论】:

    标签: iphone objective-c uiviewcontroller uisearchbar tintcolor


    【解决方案1】:

    我刚刚遇到了同样的问题。更改导航栏颜色很容易。改变 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:@"> "];
    }
    

    【讨论】:

    • 谢谢,尽管这个帖子已经很老了,但以后可能对我有用。 '珍惜时间!
    • 我后来注意到的一件事是,如果点击选择器左上角的组,然后返回联系人,搜索栏颜色会变为默认灰色。
    • 我认为“picker release”在这段代码中来得太早了,因为您在下面的行中使用了“picker”。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 2014-09-20
          • 2012-06-09
          • 2014-06-21
          • 2015-11-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-28
          • 1970-01-01
          相关资源
          最近更新 更多