iOS学习篇之更改UISearchBar的背景为透明

输入相关关键字在百度中能搜索出来很多,但是都不靠谱,于是Google了一下,在StackOverFlow中找到一个蛮靠谱的方法,试了可行。

 

with iOS8 sdks apple moved @"UISearchBarBackground" view one level deeper, so have will need to look at subviews of the child-views as bellow,.

 

for (UIView *subview in searchBar.subviews) {
        for(UIView* grandSonView in subview.subviews){
            if ([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
                grandSonView.alpha = 0.0f;
            }else if([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarTextField")] ){
                NSLog(@"Keep textfiedld bkg color");
            }else{
                grandSonView.alpha = 0.0f;
            }
        }//for cacheViews
    }//subviews

 

耶!

 

接下来,我又发现一位博主分享的文章,写的很详细,而且解决方案看起来会更加优雅,不像上面给的那样暴力粗鲁。^_^

文章在这里

这里贴出代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 1
    UIImage* clearImg = [CDViewController imageWithColor:[UIColor clearColor] andHeight:32.0f];
    // 2
    [_searchBar setBackgroundImage:clearImg];
    // 3
    [_searchBar setSearchFieldBackgroundImage:clearImg forState:UIControlStateNormal];
    // 4
    [_searchBar setBackgroundColor:[UIColor clearColor]];
}

+ (UIImage*) imageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

修改APP内全局UISearchBar样式:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIColor* myColor = [UIColor colorWithRed:0 green:0.48 blue:1 alpha:1]; // set your desired background color here

    UIImage* clearImg = [CDViewController imageWithColor:[UIColor clearColor] andHeight:1.0f];
    UIImage* coloredImg = [CDViewController imageWithColor:myColor andHeight:32.0f];

    [[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]];
    [[UISearchBar appearance] setBackgroundImage:clearImg];
    [[UISearchBar appearance] setSearchFieldBackgroundImage:coloredImg forState:UIControlStateNormal];

    return YES;
}

给ScopeBar设置透明背景:

UIImage* clearImg = [CDViewController imageWithColor:[UIColor clearColor] andHeight:1.0f];

[_searchBar setScopeButtonTitles:nil];
[_searchBar setScopeBarBackgroundImage:clearImg];
[_searchBar setScopeBarButtonBackgroundImage:clearImg forState:UIControlStateNormal];
[_searchBar setScopeBarButtonDividerImage:clearImg forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal];

 

转载于:https://www.cnblogs.com/emmet7life/p/4648383.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值