在进行ios开发的时候,有时候涉及到搜索功能,实现搜索功能的方法有不少,能够是用自定义的搜索控件,也能够用sdk提供的UISearchController(ios8之后)、UISearchDisplayController(ios8以前);ios
下面介绍UISearchController使用方法及注意事项:ide
_searchController = [[UISearchController alloc] initWithSearchResultsController:_viewController];spa
_searchController.searchResultsUpdater = self; //设置UISearchResultsUpdating协议代理代理
_searchController.delegate = self; //设置UISearchControllerDelegate协议代理orm
_searchController.dimsBackgroundDuringPresentation = NO; //是否添加半透明覆盖层开发
_searchController.hidesNavigationBarDuringPresentation = YES; //是否隐藏导航栏it
[self.view addSubview:_searchController.searchBar]; //此处重要一步,将searbar显示到界面上io
另外须要注意在合适的地方添加下面一行代码渲染
self.definesPresentationContext = YES;date
这行代码是声明,哪一个viewcontroller显示UISearchController,苹果开发中心的demo中的对这行代码,注释以下
// know where you want UISearchController to be displayed
a、若是不添加上面这行代码,在设置hidesNavigationBarDuringPresentation这个属性为YES的时候,搜索框进入编辑模式会致使,searchbar不可见,偏移-64;
在设置为NO的时候,进入编辑模式输入内容会致使高度为64的白条,猜想是导航栏没有渲染出来
b、若是添加了上面这行代码,在设置hidesNavigationBarDuringPresentation这个属性为YES的时候,输入框进入编辑模式正常显示和使用;在设置为NO的时候,搜索框进入编辑模式致使向下偏移64,具体缘由暂时未找到