不能将UISearchController与UICollectionView一起使用? [英] Can't use UISearchController with UICollectionView?

查看:693
本文介绍了不能将UISearchController与UICollectionView一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WWDC 2014演讲A Look Inside Presentation Controllers中,演示者展示了如何在UITableView中设置UISearchController。他们通过设置searchController的searchBar框架,然后将其设置为tableView的tableHeaderView来完成此操作。不幸的是,UICollectionView没有相应的tableHeaderView。使用UISearchDisplayController,这很简单:创建一个UISearchBar并将其添加到自定义UICollectionView节头,然后使用搜索栏初始化UISearchDisplayController。问题是,你不能用UISearchBar初始化UISearchController,甚至设置searchBar,因为它是一个只读属性。我想我真正的问题是,我的选择是什么?在没有UISearchDisplayController或UISearchController的情况下实现搜索是否有好方法?

In the WWDC 2014 talk " A Look Inside Presentation Controllers" the presenters showed how to setup a UISearchController in a UITableView. They do this by setting the searchController's searchBar's frame, then setting it as the tableView's tableHeaderView. Unfortunately, there isn't an equivalent of tableHeaderView for UICollectionView. With UISearchDisplayController, this would be simple: create a UISearchBar and add it to a custom UICollectionView section header, then initialize the UISearchDisplayController with the search bar. The problem is, you can't initialize a UISearchController with a UISearchBar, or even set the searchBar because it's a read-only property. I guess my real question is, what are my options? Is there a "good" way to implement search without UISearchDisplayController or UISearchController?

推荐答案


使用UISearchDisplayController,这很简单:创建一个UISearchBar并将其添加到自定义UICollectionView节头,然后使用搜索栏初始化UISearchDisplayController

With UISearchDisplayController, this would be simple: create a UISearchBar and add it to a custom UICollectionView section header, then initialize the UISearchDisplayController with the search bar

将为您创建UISearchController中的搜索栏。当您被问到数据源方法中的补充视图时

The search bar in a UISearchController is created for you. When you are asked the supplementary view in the data source method

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

将searchController.searchBar添加为子视图补充观点。别忘了打电话

add the searchController.searchBar as a subview of the supplementary view. Don't forget to call

[searchController.searchBar sizeToFit]

以便为搜索栏提供合适的尺寸。

in order to give the search bar the appropriate size.

这篇关于不能将UISearchController与UICollectionView一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆