1

我有一个UICollectionView,它有九个单元格。每个单元格都是正方形的,所以它们以3乘3的格子排列。这很好,当设备旋转时,我呼叫performBatchUpdates:completion:强制collectionView为新的方向布局单元格。如何将UISearchBar或UISearchDisplayController用作UICollectionView标头?

这里是什么样子没有搜索栏:

enter image description here

我现在尝试添加一个UISearchBar上的CollectionView的顶部,就像这样:

enter image description here

我尝试将它添加为标题视图,但不是显示在顶部,而是消耗整个屏幕,并将单元格向右推。要看到它们,您可以滚动浏览,在没有搜索栏的地方出现。

要添加搜索栏,我尝试了两种方法。两者都不能完全工作,而且似乎是一种可怕的做法。我尝试的第一种方法是将搜索栏添加为标题视图。我的主视图是UICollectionViewController的一个子类。以下是我如何设置:

- (void) viewDidLoad 
{ 

// Set up some other things... 

// 
// Register the header view class 
// which installs a search bar inside 
// of itself. 
// 

[[self collectionView] registerClass:[PDReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"]; 

} 

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 
{ 
    PDReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath]; 
    [header setFrame:[[[self navigationController] navigationBar] bounds]]; 
    return header; 
} 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section 
{ 
    if (section == 0) { 
     return CGSizeMake(collectionView.frame.size.width, self.navigationController.navigationBar.frame.size.height); 
    } 

    return CGSizeZero; 
} 

在PDReusableView内部,我安装了一个UISearchBar。这就是我得到:

A search something

所以这种方法失败了,因为我不能争吵搜索控制器或​​搜索栏为可重复使用的视图。如果我不能把它放到可重用的视图中,它就不能进入UICollectionView。

我的其他选择是调整集合视图的大小,所以它不占用整个屏幕。那么问题就变成了,我在哪里可以在搜索栏中安装搜索栏呢?最简单的地方虽然可能不正确,但它是应用程序委托,我在其中设置了视图层次结构的其余部分。我设法通过application:didFinishLaunchingWithOptions:内安装它让搜索栏出现在集合视图上方这里是我的代码:

UICollectionView *collectionView = [[self mainMenuViewController] collectionView]; 

CGRect f = [collectionView frame]; 

CGFloat height = [[[self navigationController] navigationBar] frame].size.height; 

f.size.height -= height; 
f.origin.y += height; 

[collectionView setFrame:f]; 

// Cause the cells to be laid out per the new frame 
[collectionView performBatchUpdates:nil completion:nil]; 

[[collectionView superview] addSubview:[self searchBar]]; 
[[self searchBar] setFrame:[[[self navigationController] navigationBar] bounds]]; 

虽然我可以继续搜索逻辑添加到应用程序委托,这是不恰当的这样做是因为我会维护数据集,过滤搜索结果以及应用程序委托中的许多其他逻辑。我觉得像UICollectionViewController子类是一个更合适的放置它的地方。

有没有办法在UICollectionView中安装UISearchBar而不使用Interface Builder?怎么样?

编辑:

我使用的是香草UICollectionViewFlowLayout和使用一些的委托方法来设置

+0

我觉得补充视图方法中缺少的是UICollectionViewLayout类的详细信息。你在使用自定义的子类吗?你的'-layoutAttributesForItemAtIndexPath:'和'-layoutAttributesForSupplementaryViewOfKind:atIndexPath:'方法看起来像什么? – 2013-03-24 15:15:05

+0

使用香草流布局。 – Moshe 2013-03-24 16:14:41

+0

如果你不想让你的UISearchBar和UICollecitonView一起滚动,在CollectionView之外实现它并且搜索功能变得非常容易控制 – 2013-03-26 06:28:28

回答

3

我没有用UICollectionViewUICollectionViewController但细胞的大小等,但我已经使用UITableViewUITableViewController,我的一般经验法则是:如果您想做任何复杂的事情,请不要使用UITableViewController。我怀疑UICollectionViewController是相似的:一个相当脆弱,有限的视图控制器,实际上并没有为你节省很多麻烦。

所以如果我在你的位置上,我会在我的视图控制器中寻找子类UIViewController和包括UICollectionView。您的视图层次结构可能如此简单:

Root UIView 
    UISearchBar 
    UICollectionView 
+0

看起来你可能是对的。我以前去过那里,可以完全相关。我会看看你的方法产生了什么。 – Moshe 2013-03-24 15:06:41

+0

@seamus Campbell thnx – 2014-10-15 06:21:04

相关问题