UICollectionViews and Autolayout Cells

Hi Everyone,

I am relatively new to the forums. So please be kind 🙂 I have currently posted on Stackoverflow regarding a couple of issues I am having but I thought I might have better luck posting here as well.


Objective -

UICollectionview with a custom positional layout with the cells using auto layout.

Current state of play:

I currently have a LayoutView which inherits from UICollectionViewLayout. In my PrepareLayout() I am looping around my collection of items and create a list of attributes which I then return via the LayoutAttributesForElementsInRect depending on the rectangle.

Also during the PrepareLayout I use "prototype" cells (cells that are used only to get the correct size and not part of the UI). I populate the cells with the data and then ask the cell for the minimal compressed height. The width is calculated based on how wide my current item needs to be - i.e. my layout has columns and an item spans columns so widthOfOneColumn * columns span - give me the cell width.

Issue:

So my problem that I have hit is that my cells consist of multiple labels that are multiline (0 lines). I have created constraints attached to the ContentView top, left, right and bottom but I can't seem to get the label to wrap correctly. I know that UILabel requires the PreferedMaxWidth to be set but my problem is that my cell is going to have a UIImageView beside the label so I don't think I can calculated the available space that remains.


So I am currently I am setting the Cell.Frame to be the calculated width and Max nfloat value and ask for the cells compressed height. This fixes the issue but the autolayout is now throwing a warning to say


"This NSLayoutConstraint is being configured with a constant that exceeds internal limits. A smaller value will be substituted, but this problem should be fixed. Break on BOOL _NSLayoutConstraintNumberExceedsLimit() to debug. This will be logged only once. This may break in the future."


Current Constraints:

ImageView TopAnchor = ContentView TopAnchor + 23

ImageView LeftAnchor = ContentView LeftAnchor + 10

ImageView WidthAnchor = 38

ImageView Height = 38


FirstLabel - TopAnchor = ContentView TopAnchor + 23

FirstLabel - LeftAnchor = ImageView Right + 10

FirstLabel - RightAnchor = ContentView RightAnchor -10


SecondLabel - TopAnchor = FirstLabel BottomAnchor + 8

SecondLabel - LeftAnchor = FirstLabel - LeftAnchor

SecondLabel - RightAnchor = ContentView RightAnchor -10

SecondLabel - BottomAnchor = ContentView BottomAnchor -10


So im not sure how I can proceed with this.. Any help would be greatly appricated.

Replies

Also I forgot to mention I am using Xamarin so I unfortunately can not breakpoint like the error suggests.

Are you saying that you don't know the size of the image at the time you need to calculate the height of the cell? Otherwise, if you know the image dimensions, you can use constraints between the image and the labels to have autolayout take that into account.


Also, I think you left out a (the?) key word in your description. You're setting the cell height to a "Max nfloat value"? What, specifically, are you using for that value?


In particular, if you're using the maximum double value (CGFloat is a double on all current architectures), you might try using a much smaller value, like 32000 or something of that order. It's unlikely that a cell can legally be taller than that, for the next few forseeable years, at least.

Hi Quincey,


Thank you very much for your response.


I will know the size of the image before hand - I was trying not to have any sort of calculations. Is this possible??


Aplogises - i did write this very quickly - you are correct i did leave out the word Height and I am using the nfloat.MaxValue. I am using the maximum double value. I will try that.


I did some more experiementing it appears that the warning i am getting -


"This NSLayoutConstraint is being configured with a constant that exceeds internal limits. A smaller value will be substituted, but this problem should be fixed. Break on BOOL _NSLayoutConstraintNumberExceedsLimit() to debug. This will be logged only once. This may break in the future."

is linked to my offscreen cells that i am using to calculate the height.



Encase I am doing this wrong - ill try and give more detail about the proototype cell part:


I have a list of prototype cells (different collectionviewcell) - that i create using an empty constructor, store them in a dictionary - then during the prepare - i get out the correct cell type from the dictionary change the frame of the cell to be the width of the calculated item and the height to be the Maximum value of nfloat. I then populate/configure the cell with the data that i need.


I then call cell LayoutIfNeeded which is when the error/warning occurs.


I then do SystemLayoutSizeFittingSize with the CompressedSize - this will then return back to the prepared layout with the min height that the cell would need.