njdehoog / NHBalancedFlowLayout

UICollectionViewLayout subclass for displaying items of different sizes in a grid without wasting any visual space. Inspired by: http://www.crispymtn.com/stories/the-algorithm-for-a-perfectly-balanced-photo-gallery
MIT License
1.34k stars 106 forks source link

maximumRowHeight or item height? #20

Closed jonathantrevor closed 10 years ago

jonathantrevor commented 10 years ago

If I try and layout one or two items I cannot seem to restrict the height. For example, if the image is really tall (say 1000px and only 10px wide) and I return a scaled estimated item size of 100px the layout manager still lays it out at 1000px.

Is there any way of preventing this problem? For example by providing a maximumRowHeight or perhaps have the estimated size layout closer to what Im returning?

jonathantrevor commented 10 years ago

A current workaround is to say you have 1 or 2 more items than you actually have and return fake cells and sizes for them. But that isnt ideal.

njdehoog commented 10 years ago

The behavior of the layout mechanism is that it always tries to fill the available space. The issue with implementing a maximumRowHeight is that it would break the layout mechanism's ability to do that.

Can you explain the kind of behavior that you would expect? Is this only an issue when displaying one or two items?

jonathantrevor commented 10 years ago

Im trying to lay out a number of items, between 1 and 100. I'd like them to NOT be constrained by the frame they are in (Im adding them to a parent scroll view) but for each row to roughly be around 100px (for example). So I create the collection view in a really tall frame first, layout the contents, and then resize to fit the height. This strategy works really when I have 3 or more but if I have less than 3 OR all of the images are really tall (say 1000px) then I dont get any reduction in height since the layout manager CAN layout the items and "fit" them in the row. So its not only an issue with fewer than 3 items - but thats the easiest to see it happening on.

So Im currently adding in fake small cells to force the row closer to the minmum height I specify. I guess I could also try and limit the height and do some kind of average guess of the height I'll need for, say 3, items per row.

Thanks for the quick response.

njdehoog commented 10 years ago

Thanks for the explanation. I'm not sure that this component offers the functionality that you're looking for. You might want to try and write your own custom collection view layout. This article offers a good starting point: http://www.objc.io/issue-3/collection-view-layouts.html

futuretap commented 10 years ago

I'd like to see a maximum cell size, too. Of course this would mean that the requirement of always filling the row horizontally can't be fulfilled anymore. Nevertheless this would help in the standard case of of a single picture that's zoomed up to 100% row width.