2 garaster garaster 于 2012.11.23 13:31 提问

UISearchBar与背景颜色不一致

我想要我的UISearchBar 的颜色和背景颜色一样,看不出来是衔接。但是现在的效果就是这样:

enter image description here

我希望是这样的,就是和背景融为一体的:

enter image description here

1个回答

g989_1314125
g989_1314125   2012.11.23 15:28

用这个代码:

   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;
        }
    }

我是用这个代码实现了这样的效果:你只要改一下颜色就行了。

enter image description here

enter image description here

最终效果是这样的

enter image description here

Csdn user default icon
上传中...
上传图片
插入图片