【问题标题】:Active UISearchBar makes status bar background blackActive UISearchBar 使状态栏背景变黑
【发布时间】:2014-06-29 04:19:10
【问题描述】:

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

应用程序仅适用于 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 的颜色相匹配。

谢谢!

【问题讨论】:

    标签: ios uisearchbar uistatusbar


    【解决方案1】:

    这是我用来扩展 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];
           }
         }
      }
    

    【讨论】:

    • 不幸的是没有为我修复它。我想这会在viewDidLoad?
    • 我已经更新了我的答案。我忘了包括一个方法。另外,我没有想出这个解决方案;前段时间在这里找到的,找不到原帖
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多