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.
Related
I wants create a ViewController with UIScrollView, UIStackView (Vertical), UILabel and UITableView. But my problem is that I can't set different height sizes to my Views. I did tried write in the field, but XCode erases and set back value. I did tried all options of distribuitions, but I think that my option is "Equal Spacing".
I've added two UIView to test.
I did can make my screen, but this not works on iPhone device and XCode says that it's impossible adjust constraints automatically. This is my objective:
Some fields needs different height sizes. Some labels need 3 lines, other 1 line.
You need to set a height constraint in order for your view to have a specific height within the stack view. Apple's Documentation
If you add constraints to all subviews i.e. top and bottom it will work correctly. Make sure you set translatesAutoResizingMaskIntoConstraints to false
I want the Vertical Spacing Constraint Constant value between two of my subviews to be a percentage of the screen height. This way the design looks about the same on all devices.
How do I do that in storyboard? Any well known tricks?
Perhaps there is some trick to achieve this with content hugging priority, compression resistance, or a second constraint of a lesser priority?
The solution I had in mind is to introduce a third subview to sit in between my two subviews - and set an aspect height constraint on that third subview with the superview. The two subviews would have a space constraint with the third subview with a constant of zero.
However - I don't like having a storyboard cluttered with make-belief invisible subviews.
create an IBOutlet of your vertical space constraints.then check what is the device using screen size then assign value to your constraint's constant. EX:
if(screenSize.height == 480)
{
self.verticalspaceConstraints.constant = 100 ;//this is an example
}
//like this add your other conditions
I have created a small video tutorial to get the basic idea try this
small video tutorial
hope this will help to you.
I have 2 solutions but am using the first one.
1) Put a UIView with the desired gap height between the two elements and give the gap view as proportional height to the main view of controller. This way it will increase according to device but this idea may feels complex. Because to design a complex screen you will have to put lots of gap view and it will be messy.
2) Mark the constraint in design time to be dynamic by assigning some character to the identifier property of the constraint. Create a category class of NSLayoutConstraint and in the class inherit its function and check if the identifier is started with that character and modify the value like multiplying with device scaling.
To have a adapting vertical spacing between two views, you can :
Use UIStackview and define spacing dynamically
Use Vertical Spacing Constraint between the two and change priority to 250. You need to have top constraint on the first and bottom on the second.
Use Equal Height relation between each subviews and superview. You set "proportional height" with like 0.4 on each, which let you 20% margin between. To do so, CTRL + clic on subviews then draw line to superview.
Then adjust "multiplier" making sure that the first element is the subview.
I am creating a view within IB and and attempting to have 3 UILabels evenly space horizontally across the view. I came across this on SO, https://stackoverflow.com/a/30249550/4597666. I have three UILabels, each have the height and width constrained. Here is what the IB looks like:
I constrained each centered horizontally, and the first UILabel I have the multiplier 3:1, second, 3:3, third 3:5 like the post states.
When I run on my emulator, I don't get the result that I was expecting. It appears all three UILabels are centered horizontally, and the first and third are not offset.
Is there another setting that I'm missing, or another way to properly space the views evenly?
you need to make only one change.
Constraint you set is 3.CenterX to Superview.CenterX all you need to do is interchange the value so that you constraint should look like in below image.
Alternative solution. If you want to set constraints currently you have set then change the ratio from "3:5" to "5:3" and similar for all the labels.
Result:
Hope it helps you solving your problem.
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.
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