开发者社区 问答 正文

UISearchBar与背景颜色不一致

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

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

展开
收起
爵霸 2016-05-27 11:04:50 2091 分享
分享
版权
举报
1 条回答
写回答
取消 提交回答
  • 用这个代码:

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

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

    2019-07-17 19:17:23 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
问答地址: