美文网首页
iOS更改searchBar的背景色、圆角弧度、输入框字号

iOS更改searchBar的背景色、圆角弧度、输入框字号

作者: 魔力双鱼 | 来源:发表于2019-01-17 18:18 被阅读0次

网上的好多都是比较老的方法,iOS8之前适用的。遂进行各种百度搜索,以下方法亲测有效。

//更改输入框背景色
    UIImage * searchBarBg = [self GetImageWithColor:[UIColor clearColor] andHeight:36.0f];
    [self.search setBackgroundImage:searchBarBg];
    [self.search setBackgroundColor:LIGHT_GRAY1];
    [self.search setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
    //更改输入框圆角
    self.search.layer.cornerRadius = 4.0f;
    self.search.layer.masksToBounds = YES;

    //更改输入框字号
    UITextField  *seachTextFild = (UITextField*)[self subViewOfClassName:@"UISearchBarTextField" view:self.search];
    seachTextFild.font = [UIFont systemFontOfSize:14];
- (UIView*)subViewOfClassName:(NSString*)className view:(UIView *)view{
    for (UIView* subView in view.subviews) {
        NSLog(@"======%@",subView.class);
        if ([NSStringFromClass(subView.class) isEqualToString:className]) {
            return subView;
        }
        UIView* resultFound = [self subViewOfClassName:className view:subView];
        if (resultFound) {
            return resultFound;
        }
    }
    return nil;
}

/**
 *  生成图片
 *
 *  @param color  图片颜色
 *  @param height 图片高度
 *
 *  @return 生成的图片
 */
- (UIImage*) GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
    CGRect r= CGRectMake(0.0f, 0.0f, 1.0f, height);
    UIGraphicsBeginImageContext(r.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

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

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

    return img;
}

相关文章

网友评论

      本文标题:iOS更改searchBar的背景色、圆角弧度、输入框字号

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