0

I'm working on a project that uses a UICollectionView (PSUICollectionView actually) and I've added a UISearchBar at the top of this collection view. What I now wanna do is to scroll off this search bar by default until the user pulls it down. This behavior is very easy to do in a UITableView, however UICollectionView's contentSize doesn't even get initialized until viewDidAppear!! So setting the contentOffset's Y to search bar height has no effect, unless it's done in viewDidAppear (which is not what I want). Has someone figured out using a UISearchBar with UICollectionView and how to keep it scrolled off by default?

Thanks! -Amit

amit
  • 1
  • 2
  • I answered a similar problem here http://stackoverflow.com/questions/15787754/set-minimum-contentsize-for-uicollectionview – michael23 Apr 10 '13 at 09:35

6 Answers6

0

I didn't get any response on my question but I'll post what I finally ended up doing. I used a scrollview and added the search bar and collection view as subviews to the scrollview. That's the crux of the solution but actually making it work well is quite tricky. What you'd have to do when adding collection view as a subview is to make its frame height same as its contentSize. This makes sure that collection view doesn't scroll in itself and rather the superview will handle the scrolling. Another thing to keep in mind is that unlike UITableView, UICollectionView objects don't get their contentSize updated till viewDidAppear. So I had to set a fake frame and contentSize in viewWillAppear and then readjust in viewDidAppear.

amit
  • 1
  • 2
0

It sounds to me like there's something wrong with either the way the collection view has been added to its collectionview-controller or how that collectionviewcontroller is presented by another view controller. Are you using any custom view controller containment? If so, be sure to notify childViewControllers (like your collectionView) that it has been added to a parent.

If none of these, then it could be you don't have your data objects loaded before you reach viewDidAppear. If you're loading them manually into an array, when do you call reloadData? If you're using NSFetchedResultsController check that you have fetchedObjects before the view appears, as they are needed to calculate the contentSize.

Did you subclass the UICollectionViewFlowLayout? If so, is it calling prepareLayout before the view appears?

Not much of an answer, but it sounds like your problem could be in several places.

ChrHansen
  • 1,502
  • 1
  • 14
  • 22
0

I did a similar thing by adding a delay and then calling this method:

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
Taryn
  • 242,637
  • 56
  • 362
  • 405
0

This solution worked for me

1.Make sure you click first

2.Custom Class

3.Give a identifier

enter image description here

4.

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
          viewForSupplementaryElementOfKind:(NSString *)kind
                                atIndexPath:(NSIndexPath *)indexPath
{
    return [self.imageCollection dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectionViewHeader" forIndexPath:indexPath];
}

5.On storyboard add search bar to header part.

Should work!

Eugene Loy
  • 12,224
  • 8
  • 53
  • 79
royemi
  • 83
  • 1
  • 9
0

I end up using this trick:

Adjust the inset in the viewdidload and reset it again in the viewdidappear and set the contentoffet their as well.

ViewDidLoad:

[self.mainCollectionView setContentInset:UIEdgeInsetsMake(-44, 0, 0, 0)];
firstTime = TRUE;

viewDidAppear:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];


    if (firstTime) {
        [self.mainCollectionView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        [self.mainCollectionView setContentOffset:CGPointMake(0, 44)];
        firstTime = FALSE;
    }
}
Sjoerd Perfors
  • 2,317
  • 22
  • 37
0

My solution:

  1. In viewDidLoad initially hide the search bar by setting the contentInset's top:

    self.collectionView.contentInset = UIEdgeInsetsMake(-56.0, 0.0, 0.0, 0.0)

  2. When the user scrolls down past a threshold clear the contentInset to reveal the search bar:

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.contentOffset.y < -30.0) { [UIView animateWithDuration:0.25 animations:^{ self.collectionView.contentInset = UIEdgeInsetsZero; }]; } }

Rivera
  • 10,792
  • 3
  • 58
  • 102