UISearchBar去除背景

我们使用系统提供的UISearchBar时候创建出来的搜索框是这样的。


Snip20160728_16.png

这里我把它设置成了红色的边框。默认是灰色的那种

我们觉得灰色那一坨真挫。于是想办法去除那个边框

Snip20160728_17.png

这里不需要自定义。不过这里有点特殊。特殊在iOS版本不同的话UISearchBar的子控件是不同的
iOS 7.0以上有两层subviews iOS 7.0以下只有一层 而iOS7.0刚好没有
直接上代码

Snip20160728_18.png

Snip20160728_19.png
    self.view.backgroundColor = [UIColor greenColor];
    
    UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(100, 200, 200, 40)];
    search.placeholder = @"请输入搜索条件";
    [self.view addSubview:search];
    
    //找出文本框 并且设置背景为orangeColor
    for (UIView *obj in [search subviews]) {
        for (UIView *objs in [obj subviews]) {
            if ([objs isKindOfClass:NSClassFromString(@"UITextField")]) {
                [objs setBackgroundColor:[UIColor orangeColor]];
            }
        }
        
        if ([obj isKindOfClass:NSClassFromString(@"UITextField")]) {
            [obj setBackgroundColor:[UIColor orangeColor]];
        }
    }
    
    //判断是哪个版本 7.0以上有两层subviews 7.0以下只有一层 而7.0刚好没有
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version == 7.0) {
        search.backgroundColor = [UIColor clearColor];
        search.barTintColor = [UIColor clearColor];
    }
    else
    {
        for (int i = 0; i < search.subviews.count; i++) {
            UIView *backView = search.subviews[i];
            //7.0以下只有一层
            if ([backView isKindOfClass:NSClassFromString(@"UISearchBarBackground")] == YES)
            {
                [backView removeFromSuperview];
                [search setBackgroundColor:[UIColor clearColor]];
                break;
            }
            //7.0以上有两层
            else
            {
                NSArray *arr = search.subviews[i].subviews;
                for (int j = 0; j < arr.count; j++) {
                    UIView *barView = arr[i];
                    if ([barView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
                        [barView removeFromSuperview];
                        [search setBackgroundColor:[UIColor clearColor]];
                        break;
                    }
                }
            }
        }
        
    } 

还有另外种写法 我这里就写出下面 和上面效果一样,只不过写法上看起来不是没上面这种容易懂而已

Snip20160728_20.png

顺便附上代码:

    UISearchBar *_searchBar  = [[UISearchBar alloc] initWithFrame:CGRectMake(100, 300, 200, 40)];
    _searchBar.placeholder = @"哈哈";

    [self.view addSubview:_searchBar];
    
    //当前版本号
    float version = [[[UIDevice currentDevice]systemVersion]floatValue];
    
    if([_searchBar respondsToSelector:@selector(barTintColor)]) {
        
        float iosversion7_1 = 7.1;
        
        if(version >= iosversion7_1)
        {
            [[[[_searchBar.subviews objectAtIndex:0]subviews]objectAtIndex:0]removeFromSuperview];
            [_searchBar setBackgroundColor:[UIColor clearColor]];
            
        }
        else{//iOS7.0
            
            [_searchBar setBarTintColor:[UIColor clearColor]];
            
            [_searchBar setBackgroundColor:[UIColor clearColor]];
        }
    }
    else{
        
        //iOS7.0以下
        
        [[_searchBar.subviews objectAtIndex:0]removeFromSuperview];
        
        [_searchBar setBackgroundColor:[UIColor clearColor]];
        
    }

备注:

如果有不足或者错误的地方还望各位读者批评指正,可以评论留言,笔者收到后第一时间回复。

QQ/微信:2366889552 /lan2018yingwei。

简书号:凡尘一笑:[简书]

http://www.jianshu.com/users/0158007b8d17/latest_articles

感谢各位观众老爷的阅读,如果觉得笔者写的还凑合,可以关注或收藏一下,不定期分享一些好玩的实用的demo给大家。

文/凡尘一笑(简书作者)

著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • UISearchbar去除背景色的方法 通常使用UISearchbar都需要去除其背景色来与自己的界面风格保持协调...
    凌巅阅读 6,738评论 0 0
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,541评论 4 61
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,704评论 7 249
  • 这几天河里死好多鱼,附近弥漫着腐臭味。 * 流水线有好也有不好,没什么问题的话,一天下来基本不操心,体力也不吃亏,...
    瞿桂林阅读 32评论 0 0
  • 天又黑了,这个周日要结束了。外面的路灯下看到被风刮的树来回摆动。好像不愿意去做什么。然而今夜的生活也许漫长或短暂...
    愉快的佩吉阅读 2,047评论 0 0

友情链接更多精彩内容