Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Y Constraint missing in Vertical StackView

I have a UITableViewCell that has an embedded UIStackView in it. Everything works and looks fine on the phone and iPad. However, I get a lot of weird errors in the StoryBoard and I don't know if I can ignore them or not. Typically, I hate ignoring warnings. But is it OK in this case? My Table Cell is set up as follows:

UITableViewCell
 - Content View
 -- View (named outerView)
 --- View (named dateView for top banner color)
 --- UIStackView (vertical, named mainStackView, pinned to neighbors)
 ---- UIButton (A)
 ---- UIView (B)
 ---- UIStackView (C, horizontal)
 ---- UIStackView (D, horizontal)
 ---- UILabel (E)
 ---- UIButton (F)

Everything under the mainStackView (letters A-F) are all showing an error in StoryBoard that they "Need constraint for: Y position or height". However, per the Apple documentation it says they will handle all the constraints and vertical alignment if the UIStackView is vertical orientation. Like I said, it works fine and the arrangedSubviews can be collapsed/hidden and there are no other warnings. I just don't like shipping out code riddled with warnings. What can I do to fix this, or is it a known issue?

UPDATE: Here is the MCVE as requested by the comment below. Click here and unzip the folder to get the bare bones project that will only work in Interface Builder.

like image 985
Justin Pfenning Avatar asked Dec 02 '16 13:12

Justin Pfenning


People also ask

How to Add constraints in stack view?

fill distribution creates constraints attaching the top of the first arranged subview to the top of the stack view, and the bottom of the last arranged subview to the bottom of the stack view. It also creates a constraint between the top/bottom of adjacent subviews with the spacing of the stack view.

How to use vertical stack view in swift?

To use a stack view, open the Storyboard you wish to edit. Drag either a Horizontal Stack View or a Vertical Stack View out from the Object library, and position the stack view where desired. Next, drag out the stack's content, dropping the view or control into the stack.

How to Add stack view in xcode?

Once selected, click the Embed In button in the Auto Layout toolbar at the bottom-right of the storyboard canvas. A menu with available embed options will appear. Choose Stack View: Xcode will embed the buttons in a new stack view for you.


2 Answers

I also had similar problem with the vertical stack view. The way i solved it by giving height constraint to any subview(in my case horizontal stack view) and removing it at build time. I also tried this on your MCVE and it worked.

Note: This is certainly a hack but it resolved the problem.

like image 176
Ravi Gupta Avatar answered Nov 03 '22 07:11

Ravi Gupta


The way I solved this was to either:

  1. Remove the bottom constraint on the main stack view to the bottom of the SuperView so only 3 sides were pinned.

or

  1. Adjust the row height of the table cell. Although it is a dynamic height cell based on label/button heights, I guess it still needs to be the "correct" height in interface builder?

I had to use option 2 because if I did option 1, my table cell disappeared as it did not know how the height to use for the cell.

Also worth reiterating that the code as before still worked fine, it just had compilation warnings. I have no idea why nobody else could see them despite being on the same level Xcode. Now off to try and get my 3 days back and work on my OCD about warnings in code.

like image 38
Justin Pfenning Avatar answered Nov 03 '22 08:11

Justin Pfenning