UISearchBar 的字体背景颜色属性图片替换,打造自己想要的搜索框

来源:互联网 发布:软件过程的概念 编辑:程序博客网 时间:2024/04/30 05:23

UISearchBar对于新手来说 如果需要改变里面得内容图片需要查找大量的资料,现在整理出来,以供参考

1.创建

     _mySearchBar = [[UISearchBaralloc] init];

    //_mySearchBar.frame=CGRectMake(0, 0, SCREEN_WIDTH, 40);

    _mySearchBar.delegate =self;

    //    //设置选项

        //[_mySearchBar setScopeButtonTitles:[NSArray arrayWithObjects:@"First",@"Last",nil]];

    [_mySearchBarsetAutocapitalizationType:UITextAutocapitalizationTypeNone];

    [_mySearchBar sizeToFit];

    _mySearchBar.backgroundColor =ZCColor(23,25, 28);

   _mySearchBar.backgroundImage = [selfimageWithColor:[UIColorclearColor]size:_mySearchBar.bounds.size];


//可以改变取消按钮的颜色

   _mySearchBar.tintColor=ZCColor(240,208, 122);



 _mySearchBar.placeholder=@"请输入球场名称";

    //改变输入字体的颜色

   UITextField *searchField=[_mySearchBarvalueForKey:@"_searchField"];

    searchField.textColor=ZCColor(240,208, 122);





 //提示语的字体颜色

    [searchField setValue:ZCColor(240,208, 122) forKeyPath:@"_placeholderLabel.textColor"];

    [_mySearchBarsetSearchFieldBackgroundImage:[UIImageimageNamed:@"sousuokuang"]forState:UIControlStateNormal];

    //修改提示语左边的图片

    [_mySearchBarsetImage:[UIImageimageNamed:@"sousuo_icon"]forSearchBarIcon:UISearchBarIconSearchstate:UIControlStateNormal];




这里有个灰色的蒙版必须移除不然改变不了背景色

 //移除灰色模板背景

   for (UIView *subviewin [[_mySearchBar.subviewsfirstObject] subviews]) {

        if ([subviewisKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {

            [subviewremoveFromSuperview];

           break;

        }

    }





//加入列表的header里面

    self.tableView.tableHeaderView =_mySearchBar;





下面是创建搜索框自带的点击搜索时出现得tableview,搜索结果会出现在这个控制器上,具体代码方法和用法 下篇介绍

  _mySearchDisplayController = [[UISearchDisplayControlleralloc] initWithSearchBar:_mySearchBarcontentsController:self];

    //_mySearchDisplayController = [[UISearchDisplayController alloc] init];

    _mySearchDisplayController.delegate =self;

    // searchResultsDataSource 就是 UITableViewDataSource

    _mySearchDisplayController.searchResultsDataSource =self;

    // searchResultsDelegate 就是 UITableViewDelegate

    _mySearchDisplayController.searchResultsDelegate =self;

   // [self.searchDisplayController setActive:NO animated:NO];

    

    _mySearchDisplayController.searchResultsTableView.backgroundColor=[UIColorcolorWithPatternImage:[UIImageimageNamed:@"suoyou_bj_02"]];




0 0
原创粉丝点击