0

I'm working on an app that is making use of a collecitonview. It's pretty simple as far as the colletionview goes...just displays an array of uiimages in a single-line, horizontal direction view. Here are my questions...

  1. Do I need to have a custom flow layout just to center the images when scrolling? I was able to center the first image the way I want by using insetForSectionAtIndex, however, when I scroll (horizontally), the images are aligned to the left edge of the screen. I was able to fix that by subclassing UICollectionViewFlowLayout and overriding targetContentOffsetForProposedContentOffset. However, I think there has to be a way to do such a simple task without using a custom layout. Also, using this method, while it centers the images when scrolling, it does not allow paging (no snapping the next image in place), which I would prefer. I thought that insetForSectionAtIndex would be called when scrolling through each image, but that does not seem to be the case. Is there any way to force that?

  2. The other question is sort of related to the first...how do I place the cells exactly where I want them? I have the images centered as far as the left and right edges, but I need to move the cells up (- negative) on y axis. Is overriding layoutAttributesForElementsInRect the only way to do this?

Thanks in advance!

SonnyB
  • 269
  • 3
  • 12

1 Answers1

0

In the storyboard, after you add the imageView to the contentView of the cell, use this to center it:

imageView.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
userx
  • 1,083
  • 5
  • 18
  • 36
  • Thanks...I added that to the cellForItemAtIndexPath method, but it did not make any difference. Is it supposed to go in another method? – SonnyB Jun 30 '14 at 20:56
  • It should be in cellForItemAtIndexPath. I assume that you are wiring up the imageView in the storyboard to the controller. – userx Jun 30 '14 at 21:31
  • That's correct...collectionviewcell and the imageView are wired in the storyboard. I'll play around with it a little more now that I know I have it in the right place. Thanks! – SonnyB Jul 01 '14 at 01:16
  • Not sure why it didn't seem to work at first, but your code definitely did make a noticeable difference in the positioning. However, doing it your way of setting the center of the imageView was just changing the position of the image inside the cell. So I did basically the same thing for the cell itself. – SonnyB Jul 01 '14 at 03:13
  • Sorry, I accidentally submitted too early. Using your code on the cell definitely worked, but it placed all the images on the same cell, instead of separate cells. Previously, I didn't have to set contentSize, but I guess I'll have to figure out what to set that to. Thanks! – SonnyB Jul 01 '14 at 03:27
  • Glad that it worked. If you thought my answer was helpful, you could mark it as the accepted answer. – userx Jul 01 '14 at 12:41
  • Yep, I marked it and thanks again! Although, do you have any idea why the following code is resulting in just 1 cell with all the images overlapped? cell.center = CGPointMake(self.view.frame.size.width/2, (self.view.frame.size.height/2 * .15)); I also overrode collectionViewContentSize appropriately, but still got the same result. – SonnyB Jul 01 '14 at 16:11
  • When you said the following code, which code did you refer to ? Please post it again. – userx Jul 01 '14 at 16:12
  • Here you go... cell.center = CGPointMake(self.view.frame.size.width/2, (self.view.frame.size.height/2 * .15)); – SonnyB Jul 01 '14 at 19:13
  • You need to add a looping variable here.You may be aware that the cellForItemAtindexPath gets called as mane times as there are number of items * number of sections.You need to account for that or else all the cells will end up with the same locations ( overlapping). Let me know if you need more help in understanding / coding. – userx Jul 01 '14 at 19:18
  • Yep, I understand what you mean, but not exactly sure what the code for that should be. – SonnyB Jul 01 '14 at 20:40
  • Try something like this : cell.center = CGPointMake(self.view.frame.size.width/2, (self.view.frame.size.height/2 * .15)+indexPath.row*10); – userx Jul 01 '14 at 21:03