1

我正在尝试为这个 UISearchBar 着色,使其看起来与背景无缝(如第二张图片中所示)。是否有可能做到这一点?我已经尝试在 IB 中以编程方式进行设置,但没有运气。

谢谢你的帮助

UISearchBar 截图

这是所需的外观:

UISearchBar 屏幕截图 - 所需的外观

4

2 回答 2

2
   UITextField *searchField = [self.searchBar valueForKey:@"_searchField"];
    [searchField setBackground:[UIImage imageNamed:@"searchfield_bg"]];

    [self.searchBar setTintColor:[UIColor blackColor]];
    UIImage *searchimg = [UIImage imageNamed:@"search_panel.png"];

    for (UIView *subview in self.searchBar.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
            bg.backgroundColor = [UIColor colorWithPatternImage:searchimg];
            [self.searchBar insertSubview:bg aboveSubview:subview];
            [subview removeFromSuperview];
            [bg release];
            break;
        }
    }

我使用这些图像,您也可以使用您的图像并更改颜色,我的要求是制作黑色搜索栏,所以我使用黑色

在此处输入图像描述 在此处输入图像描述

它看起来像: 在此处输入图像描述

于 2012-11-22T11:23:17.090 回答
1

像这样设置Tint颜色..

    [searchBar setBarStyle:UIBarStyleDefault];
    [searchBar setTintColor:[UIColor colorWithRed:.81 green:.14 blue:.19 alpha:1]];//set alpha with your requirement 

也与图像一起使用此代码它的工作很好..

   for (UIView *subview in searchBar.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
            bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top-bar.png"]];
            [searchBar insertSubview:bg aboveSubview:subview];
            [subview removeFromSuperview];
            break;
        }
    }

对于带有红色的整个搜索栏,然后简单地复制粘贴此代码伙伴..在这里我解决了这个问题

    for (UIView *subview in searchBar.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
            bg.backgroundColor = [UIColor redColor];
            [searchBar insertSubview:bg aboveSubview:subview];
            [subview removeFromSuperview];
            break;
        }
    }
    [searchBar setTintColor:[UIColor redColor]];
于 2012-11-22T11:26:53.950 回答