5

I would like to add a searchbar to a UICollectionViewController, that's embedded the following way: UItabbarController > UINavigationbarController > UICollectionViewController > SearchBar (!) In this view, the search bar would replace the NavigationBar.

Under the same design, if I test the above with a UITableViewController, the searchbar shows up fine (both programmatically and via the Storyboard)

Problem is I can't get to add the search bar over the UICollectionViewController when I use the StoryBoard framework; it just sits in the middle of the view, and I'm clueless as to how to move it to the top. Plus, it always appears below the UICollectionview, so it's not visible.

So, taking the other route, programmatically:

-(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];
}

With this, the search bar shows up fine. But unfortunately, when I type in some text, it disappears above the view - presumably because the underlying navBar does so - (don't know why...)

I'm not sure exactly why the searchbar is fine with a UITableViewController, and why it is such a pain for a UICollectionViewController. That said, anyone has a clue as to why the searchbar/navBar disappear, and how I can fix that ?

Any solution is welcome..

thanks ! -A

2 Answers 2

8

Add a Header and put the SearchBar in that (that is what I have done in the past). That being said, I have gotten in the habit of hardly ever using either a UITableViewController (unless I am implementing a StaticCell TableView) or a UICollectionViewController. What I would suggest is to implement a standard UIViewController and just add in your UICollectionView. Size the CollectionView down some and put the SearchBar at the top. This allows you to have a SearchBar that is always displayed (which my users generally like better than having to scroll to the top to change, edit a search)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks ! That might be a solution that I'll have to consider, I agree, as it seems the UICollectionviewController uses 100% of the view. I'd like to have the search bar hidden/visible on demand, so the navBar provided an easy way out. Doing the same with a UIViewController, while allowing the uicollectionview to resize properly will require some rework though. I'd rather avoid it if I can. for now.
In a header no, as this option isn't what I'm looking for (the search bar will scroll with the view). I might try the other - UIViewController - though I tend to think I should conform to UICollectionViewController/UICollectionView pair.
If you don't want it to scroll with the scrollview, you will need to use a ViewController and then add your CollectionView
you're right, but this is kind of adding heterogeneity in the code, making it a lot less reusable. I might have to rethink the whole stuff. I still don't get why it's working fine with UITableviewController, and so broken with UICollectionViewController. Anyway, Jay, thanks for your help !
You don't have to add it to the header in code. That can be done in Interface Builder.
8

I use the following code to add a UISearchBar to the UICollectionViewController. Unfortunately I couldn't make UISearchDisplayController working.

- (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];
}

2 Comments

Hi Eugene, thanks! In fact, this is pretty much Jay's solution, which wasn't exactly what I needed. In the interim, I found this one: stackoverflow.com/questions/10617330/…, which works fine for my needs. cheers.
In addition to this approach, you can set the layout's headerReferenceSize property as CGSizeMake(320.0,44.0) so that the content will start after point 44.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.