zoukankan      html  css  js  c++  java
  • 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;
    用这个方法还可以改取消按钮的颜色、字体什么的。


  • 相关阅读:
    模板:Prime最小生成树堆优化 + Dijkstra单源最短路堆优化
    线段树
    算法竞赛进阶指南——后缀数组
    浏览器后退刷新页面
    layer.photos()异步修改图片地址后显示异常
    img transform:scale 放大在ios下变模糊
    手机端input[type=date]的placeholder不起作用
    zepto和jQuery on事件委托在苹果手机上的”坑“
    js cookie存取
    js实现消息滚动效果
  • 原文地址:https://www.cnblogs.com/daxueshan/p/7833176.html
Copyright © 2011-2022 走看看