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

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

1.创建

     _mySearchBar = [[UISearchBar alloc] init];

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

    _mySearchBar.delegate = self;

    //    //设置选项

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

    [_mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];

    [_mySearchBar sizeToFit];

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

   _mySearchBar.backgroundImage = [self imageWithColor:[UIColor clearColor] size:_mySearchBar.bounds.size];


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

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



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

    //改变输入字体的颜色

    UITextField *searchField=[_mySearchBar valueForKey:@"_searchField"];

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





 //提示语的字体颜色

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

    [_mySearchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"sousuokuang"] forState:UIControlStateNormal];

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

    [_mySearchBar setImage:[UIImage imageNamed:@"sousuo_icon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];




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

 //移除灰色模板背景

    for (UIView *subview in [[_mySearchBar.subviews firstObject] subviews]) {

        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {

            [subview removeFromSuperview];

            break;

        }

    }





//加入列表的header里面

    self.tableView.tableHeaderView = _mySearchBar;





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

  _mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_mySearchBar contentsController: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=[UIColor colorWithPatternImage:[UIImage imageNamed:@"suoyou_bj_02"]];




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值