如果您使用的是带有UITableViewController的UISearchDisplayController,当用户点击搜索栏时,搜索栏的动画效果会取代导航栏。
我希望在UICollectionViewController顶部使用UISearchBar时也能获得同样的效果。有什么想法吗?
发布于 2012-12-06 13:08:07
我必须以编程方式将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;
}
发布于 2012-12-08 12:35:19
我刚刚收到了同样的询问,我想出了一个半生不熟但有效的解决方案,不涉及重写UISearchDisplayController。
(最终结果:一个UITableView --它响应于shouldReloadTableForSearchString--覆盖在UICollectionView之上,一旦单击search,它就会消失,结果就会显示在collectionView中。如果感兴趣,请继续阅读)
在IB中创建了一个我插入其中的UIViewController (见屏幕截图):一个用于布局目的的视图-->其中我首先放入了一个UISearchBar和显示控制器。在同一个视图中(并排),我放入了一个带有自定义UICollectionViewCell的UICollectionView。然后我放了一个UITableViewProvider (有一个自定义的UITableCell,但这不是必需的,你也可以忽略屏幕截图中的UITabBarItem和导航项目,这是无关紧要的)
我将UITableView的高度设置为0,并连接了所有的出口和代理,最终结果如下所示,当光标进入UISearchBox时,UITableView覆盖在UICollectionView上,因为输入的是shouldReloadTableForSearchString del并将结果显示在tableView中;在searchBarSearchButtonClicked上,我只需设置UICollectionView的dataSource并在它的outlet上调用reloadData。
有人会认为苹果应该推广搜索显示控制器;也许在未来的版本中?
(这是因为这是优化UISearchDisplay控制器的方式,IIRC实际上每个字符条目都有一个UITableView堆叠在一起)
(没有发布代码,因为其中涉及了相当多的内容;如果有什么不直接的地方,请询问)
发布于 2015-11-30 21:19:31
这里只有一个快速的代码-对我有效!
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let headerView = self.mainPictureCollectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "headerView", forIndexPath: indexPath)
headerView.addSubview(searchBar)
return headerView
}
https://stackoverflow.com/questions/12626913
复制相似问题