iOS UISearcBar自定义

  UISearchBar上有两个组件UISearchBarBackground和UISearchBarTextField,通过遍历实现自定义

   UISearchBar *searchBar = [[UISearchBar alloc] init];

    searchBar.delegate=self;

    searchBar.tintColor = [UIColor clearColor];

    searchBar.barTintColor = [UIColor clearColor];

    searchBar.delegate=self;

    searchBar.frame=CGRectMake(0,0,ScreenWidth-30,30);

    searchBar.placeholder=@"请输入搜索关键词";

    UITextField*searchTextField =nil;

      for(UIView* subView in searchBar.subviews) {

        subView.backgroundColor = [UIColor clearColor];

        for(UIView* ssubViewinsubView.subviews) {

            NSString* className =NSStringFromClass([ssubView class]);

            if([classNameisEqualToString:@"UISearchBarBackground"]) {//UISearchBarBackground

                ssubView.backgroundColor = [UIColor clearColor];

                ssubView.tintColor= [UIColor clearColor];

                [ssubView removeFromSuperview];

            }else  if([classNameisEqualToString:@"UISearchBarTextField"]) {//UISearchBarTextField

                searchTextField = (UITextField*)ssubView;

            }

        }

    }


    searchBar.layer.borderColor = [UIColor clearColor].CGColor;

    searchBar.layer.masksToBounds=YES;

//设置搜索(放大镜)图标

    [searchBarsetImage:[UIImage imageNamed:@"ASTTaxMap.bundle/AST_TaxMap_searchwhite"]

       forSearchBarIcon:UISearchBarIconSearch

                  state:UIControlStateNormal];


//光标样式

searchTextField = [[[searchBar.subviews firstObject]subviews]lastObject];

    searchTextField.textColor= [UIColor whiteColor];

    searchTextField.tintColor= [UIColor whiteColor];

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

    searchTextField.font = [ASFont fontWithSize:ASFontNumber14];

    [titleView addSubview:searchBar];

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容