1、设置中文:
if ([[[UIDevicecurrentDevice] systemVersion] floatValue] >= 9.0) {
[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].title = @"取消";
}else {//ios 8
[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].title = @"取消";
}
2、设置搜索栏的背景色
//在懒加载中获取不到searchField,所以在外部设置searchbar的样式
UITextField *searchField = [self.searchBar valueForKey:@"_searchField"];
searchField.backgroundColor = [UIColor colorWithRed:28.0f/255.0f green:61.0f/255.0f blue:102.0f/255.0f alpha:1.0];
单独UISearchBar
1)放在导航栏
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)]; //搜索框的取消按钮隐藏掉
self.navigationItem.titleView = self.searchBar;
单独使用searchbar可以设置让取消按钮一直显示,而使用searchController.searchBar,就不行了,即使设置了显示,它还是会自动显示和消失的。
使用UISearchController:
1)放在导航栏:
self.navigationItem.titleView = self.searchController.searchBar;
//
[self.navigationController.navigationBaraddSubview:self.searchController.searchBar];
2)放在tableView
self.tableView.tableHeaderView = self.searchController.searchBar;//放置 搜索条在 tableView的头部视图中
_searchController.hidesNavigationBarDuringPresentation = NO;//不隐藏导航栏