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