将UISearchBar添加到嵌入到NavigationController IOS6中的UICollectionviewController

Ale*_*lex 5 uinavigationcontroller uisearchbar ios uicollectionview

我想在UICollectionViewController中添加一个搜索栏,其嵌入方式如下:UItabbarController> UINavigationbarController> UICollectionViewController> SearchBar(!)在此视图中,搜索栏将替换NavigationBar.

在相同的设计下,如果我使用UITableViewController测试上面的内容,搜索栏会显示正常(通过编程方式和通过Storyboard)

问题是我在使用StoryBoard框架时无法在UICollectionViewController上添加搜索栏; 它只是坐在视图的中间,我对如何将它移到顶部毫无头绪.此外,它始终显示在UICollectionview下方,因此它不可见.

所以,以编程方式采取其他路线:

-(void)viewWillAppear:(BOOL)animated{
self.searchBarTop = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.searchBarTop setPlaceholder:@"Enter your command here"];
self.searchDC = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBarTop contentsController:self];
self.searchBarTop.delegate = self;

[[self navigationController] setNavigationBarHidden:NO animated:animated];
[self.navigationController.navigationBar addSubview:self.searchBarTop];
}
Run Code Online (Sandbox Code Playgroud)

有了这个,搜索栏显示正常.但不幸的是,当我输入一些文本时,它会消失在视图之上 - 大概是因为底层的navBar这样做了 - (不知道为什么......)

我不确定为什么搜索栏对UITableViewController很好,以及为什么UICollectionViewController会如此痛苦.也就是说,任何人都有一个线索,为什么搜索栏/导航栏消失了,我怎么能解决这个问题?

欢迎任何解决方案..

谢谢 !-一个

LJ *_*son 8

添加一个标题并放入SearchBar其中(这是我过去所做的).话虽这么说,我养成了几乎没有使用任何一个UITableViewController(除非我正在实施一个StaticCell TableView)或一个UICollectionViewController.我建议的是实施一个标准,UIViewController然后加入你的标准UICollectionView.CollectionView向下调整一些尺寸并将其SearchBar放在顶部.这允许你SearchBar总是显示一个(我的用户通常比滚动到顶部更改,编辑搜索更好)


小智 8

我使用以下代码将UISearchBar添加到UICollectionViewController.不幸的是我无法使UISearchDisplayController工作.

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.collectionView.frame), 44)];
    self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    self.searchBar.delegate = self;
    [self.collectionView addSubview:self.searchBar];
    [self.collectionView setContentOffset:CGPointMake(0, 44)];
}

- (void) viewWillAppear:(BOOL)animated{
    // to show search bar
    [self.collectionView setContentOffset:CGPointMake(0, 0)];
    // to hide search bar
    [self.collectionView setContentOffset:CGPointMake(0, 44)];
}

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES animated:YES];
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [searchBar setText:@""];
    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar resignFirstResponder];
}
Run Code Online (Sandbox Code Playgroud)