Resize Stack View - ios

I have a UILabel and a Switch on storyboard, I use Stack View to group them, however,after embedded into a horizontal Stack View, I am not able to resize the stack view but only can move it around. Why?
(I am using XCode 8 + Swift 3)
===== UPDATE ======
Thanks guys suggesting for adding constraints, but .... I added constraints, but I still would like to see the constrainted layout on storyboard instead of messed up layout.
E.g. I added constraint to leading and trailing space to be 8 pixel away from parent, but I see red line which xcode gives to indicate the actual layout is not showing in the constraint way. That's the problem, I can only drag the stack view, but I want to resize it to match my constraint so that it looks same as that in real device.

You need to add constraints to set the the placement of the StackView. To set the distance between the two controls, simply set the spacing as I've done in the image below (Spacing = 50):

You can add the width constraint to change the width.

In Interface Builder, start by dragging out a vertical stack view, and add the flowers label, image view, and edit button. Then set up the constraints as shown.

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.

Swift 4 - Issue with constraints on iPhone

I am having some major issues with my constraints, all I did was select add missing constraints and it looks fine in my storyboard and on an iPad like so,
But when I run it on iPhone, it looks like this:
What do I need to add to fix this issue...this is what I got so far:
Don't relay only on Add missing constraints , you have to make your edits after applying them
I see some constraints with leading
949,344 and 283+
and this may exceed width of some iphone screens
Simply remove any leading and trailing , and centerX the items with width constraints
also using UIStackView is good for the lower textfields
The easiest way to solve your problem with the constrains in my opinion for this type of layout is to embed your views in to Stack View. With Stack View you can easy center this views in to Screen by adding some costrains to the Stack View. With Stack Views if you need some adjustment you can do it only with few mouse clicks.
Select all your views
Click embed in the Stack View
Delete constrians that you don't need
Adjust alignment, distribution and spacing
Add trailing, leading and heigh constrian
Now it should looks good.

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.

iOS 9 Auto-Layout and View Controllers; Knowing how to put the right constraints

I am trying to understand the basics of Auto Layout and have been trying to learn with no avail. In the image I have below I have set no constraints as I always end up not doing it correctly.
For the four squares and buttons I just want them to show appropriately on all size classes and I can not get them to. I always set leading constraints for the left squares/buttons and trailing constraints for the right squares/buttons(both to the main view controller). I put horizontal spacing and vertical spacing between the squares/buttons and put bottom/top spacing on the bottom/top squares/buttons to the bottom/top layout guide.
Screenshot:
First of all select your size class to for example w:compact h:regular and than do everything else.
You can do it using stack view. It is easy, just select two of your blue views (select one then hold shift and select second) and than click stack icon:
then set stack distribution to fill equally:
and set spacing for example to 8.:
than set constrains of the stack view to 0 from left and 0 from right and copy and paste. It will look like this:
then both stack view put in a stack view:
and set spacing to 8 in the main stack view.
Finally set 20 from bottom constraint to your main stack view.
You can use same way for buttons. After that you can simply set size class to all iPhones landscape (any,compact) and do same thing again.
If you do not understand anything, just ask me :).

How to set space between two view using auto layout(in Aspect Ratio)

I have two views in a view and i have a requirement to increase the distance between those views in aspect ratio to the main view. I am using auto layout.
Thanks in advance.
One way to solve your problem is -
For your upper view give top space constraint with super view and other required constraint.
For your lower view give bottom space constraint with super view and other required constraints.
You don't really need to give vertical space because your views will be pinned with fixed space with top and bottom of super view, so if screen size will grow space between your views will automatically grow.
Important to note however: You must add the height constraint for both views and have either fixed width constraint or have Leading and trailing space constraints.
See ScreenShot
Since you have more than one view, I recommend doing the following assuming this is the result you are looking for:
STEP 1 :
select all your views
Step 2 :
go to Editor -> Embed In -> Stack View
(now it will group them together) I'm assuming that you set the width and height constraint for each of your views before doing this.
Step 3 :
once you have done that select the stack (not the individual views). but rather the entire stack (I would do this from the side menu) and set the top layout and the bottom layout.
step 4 :
Go here and make these settings for the stack as you have it selected
and that should do it.
Let me know if I should clarify anything.
UPDATE:
If you do not want equal spacing, you can still use the above method by playing around with different stacks, etc.. However, There is a method I've used in the past to get this, which is setting a multiplier instead of a number for say spacing between bottom layout and view. Here is an example that might help below:
you can play around with it to get your desired results.

Resources