UISearchController

因为要做一个带有搜索栏的页面,所以找个下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的方法:

参考文章

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容