Using constraints to define height minus a constant with storyboard - ios

I'm using layout constraints to make my views display correctly, but I'm stuck at making one view take it's parent's height minus a given constant. Is this possible using auto layout?
Further clarification: I have a view with two containers in it; one which takes 90 pixels at the top, and another one that I want to take the rest of the space, whatever that is. That is, the height of the parent container minus 90. I've been able to manage all other constraints, but not this one.

Try this constraints. it will work.
Select height constraints from pin tap and add costraint

Related

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.

UIScrollView with multiple multi-line labels and AutoLayout?

Is there a way to achieve this? I have tried literally everything and nothing has worked for me yet.
So basically what i want to do is the following: I have a scroll view with some labels in it. All the labels get their text from a server and I have set their number of lines to 0 so that they change their height according to the amount of text. However, this does not affect the scrollview content size(even though my labels have constraints set up to the bottom,top,leading and trailing of the scrollview) and the labels go off screen and I am unable to scroll down. Can someone point me in the right direction to how I would set up my constraints, my view hierarchy and etc?
Any help is much appreciated! :)
Late, but this solved it for me:
Set leading (I have a 32pt inset), trailing and top constraints. The trailing will not actually seemingly do anything..
Make the trailing Greater Than or Equal to avoid localization alert.
Finally, add a new Equal Width constraint to the label matching the scrollview. Use the constant to subtract the required padding (I used 64 due to mirror my leading inset).
And voilĂ ! The Label will align correctly both in IB and in-app.
In Scrollview the last view's bottom constraint is so important. You should set its priority to 250 and put it to Greater than or equal.
Remember you should only change the bottom constraint of the last view, which in my case it's the continue button.
I would consider using UITableView instead, it has several benefits:
It allows for reuse of cells, if all the cells look the same
It manages recycling of cells when the number of values you're getting from the server increases (decreases memory pressure when number of cells becomes substantial)
It allows for more flexibility with the content (it's quite often for design to change last second or to evolve over the course of the project)
Most importantly, UITableView support auto sizing cells (as of iOS8), you need to specify the constraints between the label and the borders of the cell
There are several resources to start with:
http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout
https://www.captechconsulting.com/blogs/ios-8-tutorial-series-auto-sizing-table-cells
http://www.appcoda.com/self-sizing-cells/
Use a container view in a scrollView
Add constraints to superview (leading, trailing, top, bottom, height,width)
Make IBOutlet of constraints that you are going to update.
Add you all labels inside that view.
Update constrains/frame of your label so that it fits the text.
How much you increase the label height you should increase the container height too.
If the label count is not fixed use custom label class to add subview.
Perhaps you should need to understand how ScrollView works in Storyboard with autolayout.

Adding multiple UILabels in view with constant height between them

I am trying to add some 5-6 UILabel's (Single line only) in a UIView using auto layouts. I have added constraints for top most & bottom most UILabel. The problem is variable screen sizes.
Just wants to know a better approach of adding those remaing (3-4) middle UILables (As I want to show equal space between all the UILabels).
Approach 1 - Add height constarint for middle lables & based on screen size change the height constraint constat at runtime.
Approach 2 - I tried adding >= and <= constarints but I am not getting the output as desired.
Any help, how can I keep constant height between all UILabels.
(Can be achieved via UIScrollView or UITableView but I dont want scrolling .. only using auto layouts:))
You can add dummy UIViews between your UILabels and set those dummy views' heights to proportional to their container.
Or you should be able to achieve that by setting your current constraints constants at runtime and then calling setNeedsLayout&layoutIfNeeded
You can't achieve that by playing with "less than" or "greater than" constraints.
Edit: You can also use UILayoutGuide to do that on iOS9.0+
You can use stack view from left window where you have button/label/view etc..... or also you can add it from constraints tools(see attached image)
If you are looking for the attached solution, please add below constraints shown in the third image.

Complex AutoLayout for Cell with dynamic size

I have a cell that contains a container with 10 subviews (two of them are simply bounds and the others are labels). The scheme looks like this.
Dynamic labels may contain huge text so the cells should conform the appropriate size to fit the content. The question is how to set up all the constraints manually... I've tried a dozen of times to do it myself but seems I'm not that good at this. The table view supports auto dimension for row height and uses custom estimated height.
In Storyboard it looks this way.
Where blue views are a subviews of View C. A grey view behind is a View B. Bold labels are static and the others are dynamic.
Demo project.
How to setup constraints?
Thank you very much in advance!
I managed to setup your constraints so that you get the result you needed. This is what I get:
I hope this is how you wanted it to look like.
Here is a link with the project.
I will try to explain how I added the constraints so that it makes more sense.
First of all, you have view B which needs to be as big as the contentView. For this you add top/bottom/left/right constraints to the superView. Because you are using automatic dimensions, if you add all constraints with priority 1000(the maximum one), you will get some error with the constraints while running. This is because, before the cell size can be calculated automatically it is zero, so the constraints crash. Therefore, I set the priority for top and trailing space with a priority of 999 so that you don't see the error log anymore. The result is the same.
Then views C needs (top or bottom)/left/right and height constraint
Then you need to add the constraints for the labels. Since you need the right ones to have multiple lines, the constraints need to specify the vertical layout for this particular case. So, you have as follows: first label: top/left to name label and right to super view. All the other have top to the previous one,and bottom to the next one.
for the labels that don't need to resize you just need leading space to parent,horizontal space to the right label and static width. Also, you will need a constraint to align the top with the label on the right.
This is the result I get:
Hope my explanation made sense, just let me know if you have questions. Good luck with your project!

Simple scroll view constraints change?

I created a scroll view in Xcode that works awesome because of this video.
https://www.youtube.com/watch?v=3PIm8-lKAYw
When I was messing around after I made it I found out that if I clicked on the scroll view and went to Show the Size Inspector or the fifth button on the right hand side of the screen I had the option to make a constraint called Top Space change in value that caused the scroll view to become bigger and smaller. I decided to see if I could find a way to change the constraint programmatically by simply using dot notation and the equaling it to an int value that I wanted.
So what I'm trying to figure out is there a simple way to change these constraints values programmatically that change the scroll view constraints values?
Without actually following that tutorial (and there being no code in your question) I'm going to make a few assumptions.
Yes, it is possible to change the scroll view's content size by manipulating constraints in your code. If you are creating the constraints in a xib or storyboard, you will need to make sure they are hooked up to IBOutlets so that you can access them in your code.
If you have 2 views arranged vertically that affect the vertical content size of your scroll view, increasing the space between these views would also increase the vertical size of your scrollview's content size. The following would increase the space between 2 views by 20 (assuming a multiplier of 1) and subsequently increase the scrollview's vertical space by the same amount.
// This is a vertical space constraint created in your xib or storyboard between 2 views that drive the content size of your scrollview
someVerticalSpaceConstraint.constant += 20;

Resources