UISearchBar 自定义处理

首先通过 KVC 获取到内部的 textField, 然后自定制处理

UITextField *searchField = [searchBar valueForKey:@"searchField"];

  

if (searchField) {

        [searchField setBorderStyle:UITextBorderStyleNone];

        //placeHolder颜色

        [searchField setValue:_placeholderColor forKeyPath:@"_placeholderLabel.textColor"];

        searchField.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);

        searchField.textColor = _textColor;

        

        //字体大小

        [searchField setValue:_placeHolderFont forKeyPath:@"_placeholderLabel.font"];

        searchField.font = _textFont;

 

        UIImage *image = _leftImage;

        UIImageView *leftImg = [[UIImageView alloc] initWithImage:image];

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

        searchField.leftView = leftImg;

    }

 

 

其他: 添加圆角(放在一个有圆角的 bgView上面)

 

 UIView *bg = [[UIView alloc]initWithFrame:CGRectMake(2022030030)];

    bg.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:bg];

    bg.layer.cornerRadius = 15;

    bg.layer.masksToBounds = YES;

    

    UISearchBar *bar = [[UISearchBar alloc]initWithFrame: bg.bounds];

    [bg addSubview:bar];

    bar.placeholder = @"搜索...";

    [bar setBackgroundImage:[UIImage imageNamed:@"clearImage@2x"]];

 

修改圆角大小

首先在 UIView 的 category 里加一个方法:
- (UIView*)subViewOfClassName:(NSString*)className { for (UIView* subView in self.subviews) { if ([NSStringFromClass(subView.class) isEqualToString:className]) { return subView; } UIView* resultFound = [subView subViewOfClassName:className]; if (resultFound) { return resultFound; } } return nil; }

用的时候:
UIView* backgroundView = [searchBar subViewOfClassName:@"_UISearchBarSearchFieldBackgroundView"];
backgroundView.layer.cornerRadius = 14.0f;
backgroundView.clipsToBounds = YES;
用这个方法还可以改取消按钮的颜色、字体什么的。


 

转载于:https://www.cnblogs.com/daxueshan/p/7833176.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值