活动UISearchBar使状态栏背景变黑

时间:2014-06-29 04:19:10

标签: ios uisearchbar uistatusbar

我在UISearchBar的顶部有一个UITableView,效果很好。我一直在尝试自定义其外观,当搜索栏处于活动状态时会出现问题 - 它会将状态栏背景颜色变为黑色。我尝试了IOS7 Status bar change to black after search is active中的建议,但它对我没用。

Example showing the black status bar

App仅限iOS 7。 UISearchBar是最小搜索样式和默认栏样式,半透明和默认barTintColor。我已经在我的AppDelegate中自定义了它的外观,如下所示:

[[UISearchBar appearance] setBarTintColor:AAColorInputBorder];
[[UISearchBar appearance] setBackgroundColor:AAColorInputBorder];
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]]; 
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]
 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                         [UIColor whiteColor],NSForegroundColorAttributeName,
                         [UIFont fontWithName:@"Avenir" size:16], NSFontAttributeName,
                         nil] forState:UIControlStateNormal];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"Avenir" size:14]];

有没有人知道如何更改状态栏背景颜色?我希望它与UISearchBar的颜色相匹配。

谢谢!

1 个答案:

答案 0 :(得分:0)

这是我用来扩展UISearchBar颜色的一点点黑客

在viewDidLoad()

if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
    self.edgesForExtendedLayout = UIRectEdgeNone;

    [self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];

方法:

- (void) removeUISearchBarBackgroundInViewHierarchy:(UIView *)view {
    for (UIView *subview in [view subviews]) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
           [subview removeFromSuperview];
           break; //To avoid an extra loop as there is only one UISearchBarBackground
        } else {
           [self removeUISearchBarBackgroundInViewHierarchy:subview];
       }
     }
  }