如何将UISearchDisplayController与UICollectionViewController一起使用?

Hil*_*ell 16 uitableview uisearchbar uisearchdisplaycontroller ios uicollectionview

如果您使用带有UITableViewController的UISearchDisplayController,当用户点击搜索栏时,它会动画以更换导航栏.

我想在UICollectionViewController顶部使用UISearchBar时获得相同的效果.有任何想法吗?

小智 2

我必须以编程方式添加 searchBar 作为 UICollectionReusableView 的子视图,我永远无法通过 IB 使其工作,因为在分配插座时我不断收到原型错误。在实现文件中添加搜索栏对我有用。

相关方法如下。

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
    _objectChanges = [NSMutableArray array];
    _sectionChanges = [NSMutableArray array];
    [self performFetch];
    searchBar = [[UISearchBar alloc]
                  initWithFrame:CGRectMake(0.0, 50.0, self.view.bounds.size.width,
                                           44.0)];
    searchBar.placeholder = @"Search for channels";
    searchBar.tintColor = [UIColor blackColor];
    searchBar.delegate = self;
}


-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    SupplementaryView *header = nil;

    if ([kind isEqual:UICollectionElementKindSectionHeader])
    {
        header = [self.collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                    withReuseIdentifier:@"reuseHeader"
                                                           forIndexPath:indexPath];

        [header addSubview:searchBar];

    }
    return header;
}
Run Code Online (Sandbox Code Playgroud)