去掉UISearchBar的那个灰色背景框

在使用那个UISearchBar的时候我个人觉得那个灰色背景框很丑,,。。所以我就想把那个给清楚掉,结果设置背景为无色,什么什么的都设置完了还是不行。然后我去网上看了一下很多人都写了那个遍历subviews这个数组。。。。可是呢还是一样的效果。然后我就各种的乱试,得出了以下两种解决的方法。


1.设置你的barTinColor和你的背景相同就看不出来那个灰色框了哈哈哈哈,这个方法就是拿来骗人的。因为肉眼看不出来所以这是一个障眼法。不建议大家使用哈。当然你要是觉得其他方法麻烦呢用这个还是可以的啦。



2.至于这种呢就取巧于那个subviews的方法了。这个方法跟我们的版本有关的,所以最先得判断版本了。iOS7.0以上有两层的subviews,以下有一层。刚刚好7.0呢就没有你可以直接设置背景颜色和那个barTinColor味无色就好了。接下来看代码

也是两种写法。

(1)

 float version = [[[UIDevice currentDevicesystemVersionfloatValue];

    

    if (version == 7.0) {

       

        _searchBar.backgroundColor = [UIColor clearColor];

        

        _searchBar.barTintColor = [UIColor clearColor];

        

    }else{

        

        for(int i =  0 ;i < _searchBar.subviews.count;i++){

            

            UIView * backView = _searchBar.subviews[i];

            

            if ([backView isKindOfClass:NSClassFromString(@"UISearchBarBackground")] == YES) {

              

                [backView removeFromSuperview];

                 [_searchBar setBackgroundColor:[UIColor clearColor]];

                break;

                

            }else{

              

                NSArray * arr = _searchBar.subviews[i].subviews;

                

                for(int j = 0;j<arr.count;j++   ){

                    

                    UIView * barView = arr[i];

                    

                    if ([barView isKindOfClass:NSClassFromString(@"UISearchBarBackground")] == YES) {

                        

                        [barView removeFromSuperview];

                         [_searchBar setBackgroundColor:[UIColor clearColor]];

                        break;

                   

                    }

                  

                }

             

            }

      

        }

    

    }


(2)

 float version = [[[UIDevice currentDevicesystemVersionfloatValue];

    

    if ([_searchBar respondsToSelector:@selector(barTintColor)]) {

        

        float iosversion7_1 = 7.1;

        

        if (version >= iosversion7_1)        {

            

            [[[[_searchBar.subviews objectAtIndex:0] subviewsobjectAtIndex: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]];

        

    }


看起来你们觉得那个更好呢?是不是感觉第二个更好些?简单的代码一目了然。。。。。其实呢为了避免有不必要的误差产生呢我还是喜欢第一种。此文仅作为参考,如有错误的地方忘给点建议改正谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值