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];

你可能感兴趣的:(iOS UISearcBar自定义)