因为要做一个带有搜索栏的页面,所以找个下search的方法
iOS7以前是UISearchDisplayController
iOS8以后UISearchDisplayController被UISearchController替代,但是考虑对iOS7的适配,还是采用以前的方法。
基本方法:
1.创建searchbar
UISearchBar* searchBar = [[UISearchBaralloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,44)];
searchBar.delegate=self;
2.设置tableview的headerview为searchbar
_tableView.tableHeaderView= searchBar;
2.把searchbar给uisearchdisplaycontroller
dc= [[UISearchDisplayControlleralloc]initWithSearchBar:searchBarcontentsController:self];
dc.searchResultsDataSource=self;
dc.searchResultsDelegate=self;
4.更改搜索背景和搜索文本背景
for(UIView*subviewinsearchBar.subviews) {
for(UIView* grandSonViewinsubview.subviews){
if([grandSonViewisKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
grandSonView.alpha=0.0f;
}elseif([grandSonViewisKindOfClass:NSClassFromString(@"UISearchBarTextField")] ){
NSLog(@"Keep textfiedld bkg color");
searchBar.barTintColor= [UIColorblueColor];
grandSonView.backgroundColor= [UIColorredColor];
}else{
//grandSonView.alpha = 0.0f;
grandSonView.backgroundColor= [UIColorredColor];
}
}//for cacheViews
}//subviews
[searchBarsetSearchFieldBackgroundImage:[UIImageimageNamed:@"矢量智能对象"]forState:UIControlStateNormal];
UISearchController的方法: