美文网首页
UISearchBar 修改背景颜色透明

UISearchBar 修改背景颜色透明

作者: 冬的天 | 来源:发表于2016-08-04 12:10 被阅读0次

    UISearchBar *search=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 100)];

    [search setPlaceholder:@"搜索"];

    [search setPrompt:@"我的搜索"];

    //    [search setBarStyle:UIBarStyleBlackTranslucent];

    //    [search setTintColor:[UIColor redColor]];

    //    [search setSearchFieldBackgroundImage:[UIImage imageNamed:@"2"] forState:UIControlStateNormal];

    //    //3自定义背景

    //

    //    UITextField *searchField = [search valueForKey:@"_searchField"];

    //    searchField.textColor = [UIColor redColor];

    //    [searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];

    //

    //

    //    //修改放大镜图片

    //    UIImage *image = [UIImage imageNamed:@"cl_tab2_gray"];

    //    UIImageView *iconView = [[UIImageView alloc] initWithImage:image];

    //    iconView.frame = CGRectMake(0, 0, image.size.width , image.size.height);

    //    searchField.leftView = iconView;

    //让背景透明

    for (UIView *subview in search.subviews) {

    for(UIView* grandSonView in subview.subviews){

    if ([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {

    grandSonView.alpha = 0.5f;

    }else if([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarTextField")] ){

    NSLog(@"Keep textfiedld bkg color");

    }else{

    grandSonView.alpha = 0.0f;

    }

    }//for cacheViews

    }//subviews

    [self.view addSubview:search];

    相关文章

      网友评论

          本文标题:UISearchBar 修改背景颜色透明

          本文链接:https://www.haomeiwen.com/subject/mxsqsttx.html