美文网首页
修改UISearchBar的背景色

修改UISearchBar的背景色

作者: 文心楠_YouHou | 来源:发表于2019-08-07 15:37 被阅读0次

     // 创建搜索框

            UISearchBar*searchBar = [[UISearchBaralloc]initWithFrame:CGRectMake(10,Is_iPhoneX_Top+22,KWIDTH-60,32)];

            searchBar.placeholder=@"想知道点什么呢";

            searchBar.delegate=self;

            searchBar.layer.cornerRadius=16;

            searchBar.layer.masksToBounds=YES;

            searchBar.barTintColor=kColor(238,238,238);

            //[searchBar.layer setBorderWidth:8];

            //[searchBar.layer setBorderColor:[UIColor whiteColor].CGColor];

            //用颜色图片来替换背景图片

            [searchBarsetSearchFieldBackgroundImage:[self imageFromColor:kColor(238, 238, 238) withSize:CGSizeMake(KWIDTH-60, 36)] forState:UIControlStateNormal]; //输入背景框

            [selfaddSubview:searchBar];

    // 将UIColor转换成图片

    - (nullableUIImage*)imageFromColor: (nonnullUIColor*)color withSize:(CGSize)size {

        CGRectrect =CGRectMake(0,0, size.width, size.height);

        UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetFillColorWithColor(context,color.CGColor);

        CGContextFillRect(context, rect);

        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        returnimage;

    }

    相关文章

      网友评论

          本文标题:修改UISearchBar的背景色

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