我正在使用旧版应用程序,无法弄清楚为什么UISearchBar无法正确显示。

我正在尝试使背景为白色或其他任何颜色,但似乎无法正确设置。如何设置背景,使其不暗,如下所示?

这是我尝试过的:

[self.searchController.searchBar setSearchBayStyle:UISearchBarStyleDefault];
[self.searchController.searchBar setBackgroundImage:[UIImage imageWithCGImage:(__bridge CFImageRef) ([UIColor whiteColor])]];

搜索栏是通过编程方式添加的,因此我无法使用属性检查器。

或者,是否有一种方法可以通过NSLog打印样式的搜索栏属性?

ios - UISearchBar属性-LMLPHP

最佳答案

每个搜索栏视图都包含一个文本框,在这里您可以看到该文本框的背景色。默认情况下,它使用tintColor。

要手动设置,您需要创建一种方法来递归查找文本字段,然后在其上查找背景色。

- (UITextField*)searchSubviewsForTextFieldIn:(UIView*)view
{
    if ([view isKindOfClass:[UITextField class]]) {
        return (UITextField*)view;
    }
    UITextField *searchedTextField;
    for (UIView *subview in view.subviews) {
        searchedTextField = [self searchSubviewsForTextFieldIn:subview];
        if (searchedTextField) {
            break;
        }
    }
    return searchedTextField;
}

然后将此方法称为:
 [[self searchSubviewsForTextFieldIn:self.searchController.searchBar] setBackgroundColor:[UIColor whiteColor]];

希望它能工作!

10-08 06:57
查看更多