Adjusting UIView Height Inside UIStackView Control - ios

I am using a UIStackView in iOS 9 SDK. The height of the stackview is 44 points. I have a UILabel and UIView inside the StackView as shown below:
Now, I want to make the green view much smaller than 44.. much like 20. How can I do that?
Without the UIStackView I am getting breaking constraints:
[GT.BubbleView:0x14536610] (Names: '|':UITableViewCellContentView:0x14536fd0 )>",
"<NSLayoutConstraint:0x146b7300 GT.BubbleView:0x14536610.trailing == UITableViewCellContentView:0x14536fd0.trailingMargin>",
"<NSLayoutConstraint:0x146c4000 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x14536fd0(286)]>")

First you need to add UIStackView on the cell
Need to add constraints to your UIStackView
Adjust your UIStackView
Drag&Drop UILabel into your UIStackView
Then Drag&Drop UIView into your UIStackView
Add green-UIView like subview to pink UIView
Now you can add couple constraints to green-UIView
Now you can run app on simulator or device
Notice that I don't added any constraints for UILabel.

If you are trying to achieve a 20x20 square like the one below:
Then all you have to do is add another UIView to your green view. Make that UIView's size 20x20pts. Then change the small view background to green and the larger view's background to clear.
That said, this solution doesn't require a stackview and possibly over-complicates your UI. I would recommend just using autolayout instead.
Stackviews are most useful when
you want to compensate for a change in device orientation,
want to stack a lot of things on top of each other,
want to create a very complicated layout with many elements but that has
patterns you can repeat, or
simply want to be able to remove an element or add an element in with the sizing correcting itself
That list isn't exhaustive, but should give you an idea of when and why you'd use a stack view.
Outside of a prototype cell
This behavior is trivial. You can simply set the constraints on your view:
'
And then set the constraints on the stackview:
Hope this helps, cheers!

The breaking constraint problem can be easily fixed by setting the priority of width and height constraints to 800 or above.

I wanted to post my answer here so others could see, despite a slightly different problem from OP's.
I was trying to resize a UIView that contained a UIStackView and the constraints were breaking. I was changing the UIView's height constraint between 0 and 66 depending on if there was relevant info to show that the stack view was displaying for.
I was able to fix the breaking constraints by removing top and bottom constraints of the stack view inside the view, and instead set it to be vertically centered. This resulted in much smoother scrolling in my table view.

Related

UISTackview constraint error in a UIScrollview in viewcontroller

I was trying to add a UISTackview in a scrollview. I believe the UIScrollview setup is right as the following screen has scrolled. Please note that it does not have any stackview, I have added a button with big top constraint so that I can see whether the scrollview works or not.
Next, I added the stackview and just added the top, left, right constraint but not bottom constraint like the following image. However, since there is no bottom constraint, the scrollview does not work.
When I added the bottom constraint, it shows the error like the following image because none of the textfields has height constraints.
If I change the distribution to "Fill Equally". The textfields are taking too much blank spaces depending on the screen size.
How can I use the stackview inside the scrollview. I want to use it as I want some of the views hidden based on conditions and claim the space. Any suggestion would be helpful. Thanks
Your second image is correct - but you need one more constraint.
Add a constraint from the Bottom of the Stack View to the Bottom of View (its superview), but set it to >= 0
A couple of tips...
rename your UI elements... it gets very confusing to refer to elements by default generic names (you have more than one View, for example).
give your objects contrasting background colors during development... makes it much, much easier to see the frames.

UIStackViews inside UIStackView giving errors

I want to place 2 Horizontal UIStackViews inside another Vertical UIStackView. The main stack view is in the contentView of a UITableViewCell. Each of the nested stack views has 2 UIViews. The alignment is set to 'Fill' and distribution is 'Fill Equally'. For the main stack view both of them are set to 'Fill'. All of the UIViews has 2 elements, which are connected with constraints. When I place them in the storyboard I'm getting errors 'Need constraints for: Y position, height'. What am I doing here wrong?
EDIT: Also I'm hiding the UIViews depending if they have data from server or no. If both UIViews dont have data, then I hide the whole stackView.
You are missing vertical constraints for some of your views. Just because the stack view is set to "fill" does not mean the autoconstraints engine will know how tall to make each view. See if you can match the same constraints portrayed in the following screenshot and let me know if that solves your issue; these constraints make the views have equal height.
Need constraints for: Y position, height
this means that you place a view ( subviews of the inner stacks ) with no intrinsic content size so make sure to give it a height ( unlike label and button which already have ) and hook properly from top to bottom with their superviews

making an auto layout of UIimageView and textview inside scrollview for iOS 10.2 in xcode8

I am making a note taking app in xcode8 for iOS 10.2. The note is consist of a textfield, textview and imageview. I am adding textview and imageview in separate scrollviews (to provide the facility of zooming and adjusting image). Then I have added both scroll views in a stackview to ensure that imageview and textview stands side by side in a landscape mode. To do that, I have added variation to axis property of stackview( width=any, height= compact and Gamut=any, and set that to the horizontal, as shown in right corner of screenshot to see view hierarchy without any constraints).
I have tried many constraints but, every time it fails to adopt. Let's take an example, I am adjusting size of the scrollviews on the size of content(by setting top,bottom,leading and trailing constraint), the stack view is adjusted to the main view. Now, the only thing that needs to be adjusted is text view and imageview with respect of stack view. But, here is the tricky part If I include a fixed trailing constraint of text view to the stack view; it will work in portrait mode.As soon I turn into landscape mode that constraint will not work(the trailing constraint which have 0 distance from stackview) because the edge will be side to the imageview not to the stackview(because I am putting image and text side by side as explained above) which will make textview larger than required, same goes for imageview's leading constraint to the stack view and vertical distance constraint betweem image view and textview.I think the root of the problem is I have made a variation to the axis of a stack view (in order to get text and image side by side in landscape mode) but, that was necessary part.Can anyone have solution for this? Do you think there can be other problem? Should I choose Intrinsic Size of scrollviews to placeholder option or let it be system default?
Thank you.
So I hope I understand the question and I will try to walk you through what I understand you hope to accomplish. I preface this by saying I just started using UIStackView because of backwards compatibility.
Note. To get the magic of ScrollView with AutoLayout I almost always embed them in another UIView. There are reasons to not but in this case you will see how valuable this is to AutoLayout.
Step 1) Drag your UIStackView and add Top,Bottom,Leading, and Trailing. Now add 2 UIViews and set the UIStackView to Fill Equally. It will now look like the image(Background colors to check your work).
Step 2) Now add a UIScrollView to each of these UIViews. Add Leading,Trailing,Top,and Bottom on each of these.
Step 3) Add a UITextView and UIImageView to the ScrollViews respectively. Now Autolayout is mad at us :( but we will fix that.
Step 4) Drag from the UIImageView to the View that is holding the UIScrollView(First Set of Views we added). See image
-Choose the option to add Equal Widths. See Image
Repeat the same step but add Equal Heights. Now add Top, Bottom, Leading, and Trailing to the ScrollView.
Step 5) Repeat exact Step 4 with UITextView
Your final view hierarchy should look like this.
Now you can rotate your UIStackView and do what you want I think.
And Horizontal
Side by Side Preview
I did add a <= 0.1 equal heights multiplier on the textfield at the top but I don't know if that was necessary.
Enjoy.

AutoLayout: how to constrain UIView to be no taller than the height needed to contain all its subviews?

There are a few answers on SO that seem to address the same question, such as this one. However, none of these worked.
The goal is to use AutoLayout and Storyboard to create a UIView, call it parentView, that is tall enough to contain its subviews and no taller.
This can easily be done in code, but the goal is to do it all within Storyboard with AutoLayout constraints and without having to specify a static height (e.g., assume one of the subviews is a UILabel so parentView would need to grow correspondingly if the UILabel's height grows because of long text).
Is this possible?
This can actually be accomplished not with the addition of constraints, but the lack thereof!
Drag a new View into the superview and give it two constraints: Center X and Center Y. Drag some views/labels and add them to the View.
In order for this to work, the subviews inside must have constraints to the leading/trailing/top/bottom of the View holding everything together. This "defines" constraints for the View's width and height so XCode doesn't get mad at you.
That's it. Example project.
Edit: Image of the view and constraints

iOS Auto layout problems with spacing

I'm trying to place UIImage, UITextView and UIView one after another vertically.
I've set all constraints and spaces between views. But it says that it need constraints for Y position or height for UIView and UITextView.
What have I done wrong?
Here is screenshot: http://s019.radikal.ru/i616/1509/63/f18ed2a349f3.png
Generally these problems arise from putting a UIImageView itself with constraints. Your best bet is to put in a view and embed the image inside the view. This should make your life a lot easier. Just reset constraints and then click add missing constraints on the whole controller. :-)

Resources