Xcode Interface Builder 9.1 Collection View Cells have too small “expected height”

戏子无情 提交于 2019-12-04 16:20:49

问题


Recently, I encountered a problem with the Interface Builder which I failed to solve. I'm using a UICollectionViewController and whenever I add a Cell it immediately shows a orange rectangle (indicating a auto-layout update). Updating the frame/cell doesn't change anything. The warning associated with the orange marking reads "Expected: height=Y, Actual: height=X" where Y is always exactly X-100. That is: When I set a custom cell height of 193, it says "Expected: height=93". This happens no matter which size I enter.

The problem with this is that I have many difficulties with the subviews and their constraints. For instance, if I add a subview with the constraint to keep the same height as the cell, it will be shown as 93 (the expected height), even though my cell should be 193. Layouting becomes impossible.

I tried adding a new cell, same issue. I tried removing every subview and re-adding them, nothing.

Any ideas are highly welcome.

Thank you very much!


回答1:


Solution found ! (Xcode 9.2)

The bug seems to only happen when you try to embed the UICollectionViewController in a Container View in the storyboard. The workaround is to remove the segue while you set your constraints, and re-add the segue once your constraints are well set.

I also think that setting UICollectionViewController size as Freeform could be responsible of the bug. If so, try to set constraints before changing the size.

Hope this helps !




回答2:


I'm not sure if this solved it, but the warning and issue disappeared once I did the following:

Select the CollectionViewController in Interface Builder and set the size to freeform and make the height larger, large enough to show all your template cells. (Plus: maybe refresh the views). That solved the issue for me.




回答3:


In my case, I have added two collection view cell, withing collection view Controller.Only setting the view controller to free form wont help us. Need to update the size, to get rid of the warning.

updating collection view cell frames, wont help us. Warning will be continuously changing its length.

I tried to work without considering the warning, as I have set my collection view cell size programmatically and it does working as expected.




回答4:


I think it's just a strange bug I delete the collectionViewController in SB, then create a new one, the problem disappear




回答5:


This is a common bug with Interface Builder. I don't know the exact cause, but I believe it's something to do with placing collection views inside arrangement views that do not have an exact size. I've seen it when putting them in scroll views, stack views and container views.

There is a simple fix, although I believe this to be a bug in IB that ought to be corrected by Apple rather than hacking around it. The solution (for me at least - your mileage may vary!) is to place the collection view inside an ordinary UIView and pin its edges to it, then place that parent view in the position that you wanted your collection view with all the constraints it required.

I usually found that there were actually no auto layout issues at run-time, which is why I believe this to be purely an Interface Builder bug.



来源:https://stackoverflow.com/questions/47400588/xcode-interface-builder-9-1-collection-view-cells-have-too-small-expected-heigh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!