UISearchController的简单应用以及searchBar背景颜色的修改

  最近在学习UISearchController,在这里,就分享一下自己的总结 因为searchBar 自带颜色,所以想要修改,就在网上找了一些资料,最后达成了自己想要的结果


//
// ViewController.m
// UI_UISearchController
//
// Created by Summer on 16/9/8.
// Copyright © 2016年 Summer. All rights reserved.
//

#import "ViewController.h"

static NSString *const kReusableCellIdentifier = @"identifier";

@interface ViewController () <UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating,UISearchBarDelegate>{

NSMutableArray *_dataSource; /**< 数据源 */
NSMutableArray *_searchResults; /**< 搜索结果 */
}

@property (nonatomic, strong) UITableView *tableView; /**< 表格视图 */
@property (nonatomic, strong) UISearchController *searchController; /**< 搜索控制器 */

- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self initializeDataSource];
[self initializeUserInterface];
}

#pragma mark *** Initialize methods ***

- (void)initializeDataSource {
// 初始化数据源
_dataSource = [NSMutableArray arrayWithArray:[UIFont familyNames]];
}
- (void)initializeUserInterface {

self.view.backgroundColor = [UIColor whiteColor];

// 关闭系统自动偏移
self.automaticallyAdjustsScrollViewInsets = NO;

// 加载表格视图
[self.view addSubview:self.tableView];

// 添加导航栏
self.tableView.tableHeaderView = self.searchController.searchBar;

}

#pragma mark *** <UITableViewDelegate, UITableViewDataSource> ***

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// 如果用户正在搜索,则返回搜索结果的count,否则直接返回数据源数组的count;
if (self.searchController.active) {
return _searchResults.count;
}else {
return _dataSource.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReusableCellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kReusableCellIdentifier];
}

// 如果用户正在搜索,则返回搜索结果对应的数据,否则直接返回数据数组对应的数据;
if (self.searchController.active) {
cell.textLabel.text = _searchResults[indexPath.row];
}else {
cell.textLabel.text = _dataSource[indexPath.row];
}
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// 取消选中
[tableView deselectRowAtIndexPath:indexPath animated:YES];

if (self.searchController.active) {
NSLog(@"%@", _searchResults[indexPath.row]);
}else {
NSLog(@"%@", _dataSource[indexPath.row]);
}

// 停止搜索
self.searchController.active = NO;
}
#pragma mark *** <UISearchResultsUpdating> ***

// 每次更新搜索框里的文字,就会调用这个方法
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {

// 获取搜索框里地字符串
NSString *searchString = searchController.searchBar.text;

// 谓词
/**
1.BEGINSWITH : 搜索结果的字符串是以搜索框里的字符开头的
2.ENDSWITH : 搜索结果的字符串是以搜索框里的字符结尾的
3.CONTAINS : 搜索结果的字符串包含搜索框里的字符

[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,也不区分发音符号。

*/

// 创建谓词

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH [CD] %@", searchString];

// 如果搜索框里有文字,就按谓词的匹配结果初始化结果数组,否则,就用字体列表数组初始化结果数组。
if (_searchResults != nil && searchString.length > 0) {
[_searchResults removeAllObjects];
_searchResults = [NSMutableArray arrayWithArray:[_dataSource filteredArrayUsingPredicate:predicate]];
} else if (searchString.length == 0) {
_searchResults = [NSMutableArray arrayWithArray:_dataSource];
}

// 刷新表格视图
[_tableView reloadData];
}
#pragma mark *** Getters ***

- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64) style:UITableViewStylePlain];
// 设置代理
_tableView.delegate = self;
// 设置数据源
_tableView.dataSource = self;
// 设置单元格高度
_tableView.rowHeight = 50;
}
return _tableView;
}


- (UISearchController *)searchController {
if (!_searchController) {
// 初始化搜索控制器
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
// 设置代理
_searchController.searchResultsUpdater = self;
// 设置半透明背景,当设置当前视图控制器作为搜索结果的视图控制器时,要设为NO;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.searchBar.placeholder = @"搜索";
// 隐藏导航栏
_searchController.hidesNavigationBarDuringPresentation = YES;
// 关掉自动大写锁定
_searchController.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
// 设置searchBar的frame
_searchController.searchBar.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 44);
_searchController.searchBar.delegate = self;

//修改searchBar背景颜色和文本框颜色

/**< 方法1 */
/*
UIImage* searchBarBg = [self GetImageWithColor:[UIColor clearColor] andHeight:32.0f];
//设置背景图片
[_searchController.searchBar setBackgroundImage:searchBarBg];
//设置背景色
[_searchController.searchBar setBackgroundColor:[UIColor whiteColor]];
[self setSearchBarTextField];*/

/**< 方法2: 修改背景颜色,和文本框的颜色 ,比较直接 */

for(UIView *view in [[[_searchController.searchBar subviews] objectAtIndex:0] subviews]){
if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
view.alpha = 0.0f;
}else if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")] ){
view.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
}
}


}
return _searchController;
}
- (UIImage*) GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
CGRect r= CGRectMake(0.0f, 0.0f, 1.0f,height);
UIGraphicsBeginImageContext(r.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, r);

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return img;
}

//设置字体颜色,文本框的放大镜
-(void)setSearchBarTextField{
UITextField *searchField = [_searchController.searchBar valueForKey:@"_searchField"];
searchField.textColor = [UIColor blackColor]; //字体颜色
searchField.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1]; //背景颜色
//修改左边的放大镜
UIImage *image = [UIImage imageNamed:@"0"];
UIImageView *iconView = [[UIImageView alloc] initWithImage:image];
iconView.frame = CGRectMake(0, 0, image.size.width , image.size.height);
searchField.leftView = iconView;
}


@end


修改cancel为中文取消的办法,图片上已经圈出来了,添加简体中文即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值