iOS收藏

UISearchController浅析-下篇

2016-08-25  本文已影响6041人  杭研融合通信iOS

     UISearchBar+UISearchDisplayController这个组合的稳定性经过几次iOS版本迭代肯定不言而喻,但苹果爸爸就是任性的在iOS8.0中宣布弃用UISearchDisplayController,推荐开发者使用UISearchController。虽然没有找到官方或者大神的明确解释,但是在使用过程中的确发现了一些不足之处,不过笔者是用了倒推法。一开始就采用UISearchController是实现搜索功能,然后偶然发现iOS8.4版本中UISearchController竟然是失效的,苹果也是给开发者挖了一个大坑然后跑路的节奏。于是乎为了做iOS8.4版本的兼容回去使用了UISearchBar+UISearchDisplayController。


UISearchController不同于UISearchDisplayController的最大之处在于UISearchController继承自UIViewController,是一个实实在在的视图控制器,而UISearchDisplayController继承自NSObject,是一只“披着羊皮的狼”,说白了是一个工具类。通过视图层次分析可以清晰的发现两者的差别。视图层次分析分别截取了三种状态:

(1)状态一:打开程序未触发任何操作状态;

(2)状态二:选中searchbar但未输入任何内容状态;

(3)状态三:输入内容显示搜索结果状态。

UISearchController:

状态一 状态二 状态三

UISearchDisplayController:

状态一 状态二 状态三

状态一的时候,两者的视图层是类似的,UISearchBar都依附在UINavigationController下的UIView中。

从状态二开始两者就出现了根本的差别,使用UISearchDisplayController时会在UIView中添加UISearchDisplayControllerContainerView这个类,用来显示搜索结果,因为在初始化方法initWithSearchBar:contentsController:中第二个参数通常都会使用self来赋值,那么其实就是利用原来的视图控制器去显示结果,那么只有设置searchResultsDelegate=self和searchResultsDataSource=self,并且实现其代理方法才能正常显示。同时为了实现UISearchBar输入事件的监听,还需要设置delegate=self并实现其代理方法。示例代码如下:

_searchDisplayController= [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];

_searchDisplayController.delegate=self;

_searchDisplayController.searchResultsDelegate=self;

_searchDisplayController.searchResultsDataSource=self;

由于设置了过多的代理给了原视图控制器,一旦原视图控制器需要实现其他代理方法(例如原视图控制器中有一个UITableview,也需要实现它的代理方法),这样就会导致需要来回的设置delegate=self。而且实现搜索功能的逻辑代码放在原视图控制器中,会极大增加代码的复杂性和耦合性。我们提倡一个类只实现一种功能,显然苹果爸爸也是这么想的。

而UISearchController在状态二中则是在UIWindow下添加UITransitionView并覆盖了原视图,此时UISearchBar已经成为UITransitionView的子视图。状态三显示搜索结果的tableview也是UITransitionView的子视图,与原视图没有任何交集。这种方法可以很好解决上面提到的问题,将实现搜索功能的逻辑代码放在一个新的视图控制器里,这个新的ViewController是初始化在UISearchController中,不会对原视图控制器造成影响。示例代码如下:

_searchResultTableVC= [[SearchResultTableVC alloc]init];

_searchResultTableVC.delegate=self;

_searchController= [[UISearchController alloc] initWithSearchResultsController:_searchResultTableVC];

_searchController.searchResultsUpdater=_searchResultTableVC;

[self.view addSubview:_searchController.searchBar];

_searchController.searchBar.delegate=self;

这种处理方式使得代码逻辑更加清晰,有效降低功能模块之间的耦合性,视图之间也不会相互影响。

UISearchDisplayController另一个不足之处是在使用UISearchBar输入内容时,会默认把UINavigationBar隐藏起来。假如不想隐藏,则需要自定义一个类继承自UISearchDisplayController,并且重写setActive:animated:方法。代码如下:

- (void)setActive:(BOOL)visible animated:(BOOL)animated {

[supersetActive:visible animated:animated];

[self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];

}

而使用UISearchController时不想UINavigationBar隐藏,只需要设置hidesNavigationBarDuringPresentation=NO就可以,多方便!!!

UISearchController自带UISearchBar,这点也很重要!!!


开发过程中遇到过的坑

现象1:UISearchBar隐藏在屏幕顶部,由于被NavigationBar遮住而无法显示的问题。

分析原因:自iOS7.0开始,UIViewController中增加了一个新的属性edgesForExtendedLayout,用于指定边缘要延伸的方向:

edgesForExtendedLayout属性

可选值如下:

UIRectEdge值

默认值是UIRectEdgeAll,就是视图边缘向四周延伸,即使视图顶部有NavgationBar,底部有TabBar,视图也会延伸到屏幕的顶部和底部,在视图上添加View也会从屏幕的顶部开始计算高度,而不是从NavgationBar的底部,所以通常在NavigationController视图控制器中添加View需要将初始化高度增加状态栏高度(20)和NavigationBar高度(44)。

解决方法:从ios7开始,如果想从NavgationBar的底部开始添加View,那么可以设置self.edgesForExtendedLayout =UIRectEdgeNone,这样UISearchBar就可以正常显示在NavgationBar底部,其他添加其他View也同理。

还有几个与NavgationBar显示有关的属性,具体可参考博客:

http://blog.csdn.net/zyzxrj/article/details/47832337

现象2:即使设置了self.edgesForExtendedLayout =UIRectEdgeNone,UISearchController中的UISearchBar依然无法正常显示。

分析原因:在一篇博客中找到了上述方法无效的原因,只要navigationBar.translucent的值为NO(默认),self.edgesForExtendedLayout的赋值都会失效。只有当translucent的值为YES的时候,系统才会去使用edgesForExtendedLayout的值。

解决方法:实现UISearchControllerDelegate中的两个代理方法:

UISearchControllerDelegate代理

博客地址:

http://blog.csdn.net/mayerlucky/article/details/50699975


最后一次简单总结:

不知不觉已经将UISearchController拆分成上中下三部分来讲解,干货不是很多,感觉自己理解的也不够深入,希望对大家开发有所帮助,谢谢!

附上Demo的Git地址:https://github.com/MrSuperJJ/GDMapPoiDemo

上一篇 下一篇

猜你喜欢

热点阅读