UISearchDisplayController 和 UISearchBar

一、UISearchBar 的界面

   1、属性searchBarStyle对应的界面

   2、设置UISearchBar的背景色

     代码:

      searchBar.backgroundColor = [UIColor blackColor];

     效果:

    


    如果想设置默认情况下的背景色,则可以通过设置背景图片来实现

    先添加方法:

    

- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size

{

    CGRect rect = CGRectMake(0, 0, size.width, size.height);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillRect(context, rect);

    

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    return image;

}

    再添加代码:

    searchBar.backgroundImage = [self imageWithColor:[UIColor blackColor] size:searchBar.bounds.size];

    

   

   2、设置UISearchBar的文字的颜色

   方法一:

        

     for (UIView *subView in self.searchBar.subviews)

     {

        for (UIView *secondLevelSubview in subView.subviews){

            if ([secondLevelSubview isKindOfClass:[UITextField class]])

            {

                UITextField *searchBarTextField = (UITextField *)secondLevelSubview;

                //set font color here

                searchBarTextField.textColor = [UIColor colorWithWhite:0.799 alpha:1.000];

                break;

            }

        }

     }

    方法二:

 [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];


一、UISearchDisplayController用法

  

    mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];

    mySearchDisplayController.delegate = self;

    mySearchDisplayController.searchResultsDataSource = self;

    mySearchDisplayController.searchResultsDelegate = self;


   

注:UISearchDisplayController 的结果显示依赖于他的代理的tableView样式,在searchBar的代理方法中直接对数据源的数据进行改变。最后重新加载数据

    [mySearchDisplayController.searchResultsTableView reloadData];


UISearchDisplayController 将要消失的时候,把之前的数据重新赋值给数据源调用

    [_tableView reloadData];

即可还原搜索前的数据。

另外,为了保证健壮性,最好是在tableview的cellForRow方法中加个判断

  if (_dataArr.count > indexPath.row){

      ******     

  }














  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值