I need to implement a news feed like instagram but I'm facing some troubles with my constraints.
I have a tableView cell with: imageView, label 1, label 2, view 1 and view 2 in a vertical order like this:
I need to achieve the following conditions:
- my image is square.
- label 1 (the black one) have 5 lines max
- label 2 (the red one) don't have a max number of lines
- view 1 is 30px height
- view 2 is 35px height
My constraints are:
Image:
trailing space to superview
leading space to superview
top space to superview
botton space to label 1 with constant=8
height = 320 with priority=999 (during runtime I change this value to view's width)
Content hugging - Vertical = 751, Horizontal=751
Content compression - Vertical = 1000, Horizontal = 751
Label 1:
trailing space to superview with constant=-20 and priority=999
leading space to superview with constant=20
height >=20 with priority= 750
bottom space to label2 with constant=8
Content hugging - Vertical = 1000, Horizontal=752
Content compression - Vertical = 1000, Horizontal = 751
Label 2:
trailing space to superview with constant=-20 and priority=999
leading space to superview with constant=20
height >=20 with priority= 750
bottom space to view1 with constant=8
Content hugging - Vertical = 1000, Horizontal=752
Content compression - Vertical = 1000, Horizontal = 751
View 1:
trailing space to superview
leading space to superview
botton space to view2
height = 30
Content hugging - Vertical = 1000, Horizontal=250
Content compression - Vertical = 1000, Horizontal = 750
View 2:
trailing space to superview
leading space to superview
botton space to superview
height = 35
Content hugging - Vertical = 1000, Horizontal=750
Content compression - Vertical = 1000, Horizontal = 750
During runtime I receive several warning about constraints. It can't satisfy all my constraints.
"<NSLayoutConstraint:0x7fec35712d60 V:[UIView:0x7fec357391e0(30)]>",
"<NSLayoutConstraint:0x7fec3570a8f0 V:[UIView:0x7fec357432a0(35)]>",
"<NSLayoutConstraint:0x7fec350d17b0 V:|-(0)-[UIImageView:0x7fec35711340] (Names: '|':UITableViewCellContentView:0x7fec35744f30 )>",
"<NSLayoutConstraint:0x7fec350bf830 V:[UIImageView:0x7fec35711340]-(8)-[TimelineLabel:0x7fec3570a980'Usura']>",
"<NSLayoutConstraint:0x7fec350bc7e0 V:[TimelineLabel:0x7fec3570a980'Usura']-(8)-[UILabel:0x7fec350ddb40'Produtos: BATOM: MAC, SOM...']>",
"<NSLayoutConstraint:0x7fec35701f20 V:[UILabel:0x7fec350ddb40'Produtos: BATOM: MAC, SOM...']-(8)-[UIView:0x7fec357391e0]>",
"<NSLayoutConstraint:0x7fec350e90c0 V:[UIView:0x7fec357391e0]-(0)-[UIView:0x7fec357432a0]>",
"<NSLayoutConstraint:0x7fec350e9110 UIView:0x7fec357432a0.bottom == UITableViewCellContentView:0x7fec35744f30.bottom>",
"<NSLayoutConstraint:0x7fec357085c0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fec35744f30(0)]>"
Will attempt to recover by breaking constraint
NSLayoutConstraint:0x7fec3570a8f0 V:[UIView:0x7fec357432a0(35)];
For each cell in my tableView break a different constant.
I tried a lot of solutions but none works.
Help! Please!
The warnings are about unbreakable constraints in a dynamic TableViewCell. But as your layout changes in execution time, you must let your view adapt itself without strict "pixel accuracy" constraints.
You need to set down some of your 1000 priority constraints to 999. This will make Xcode happy about some cases that breaking constraints are needed. In most cases, however, all your constraints will be satisfied. =D
Related
My task is to preserve the size and position of the elements inside my cell for different screen resolutions.
I did:
Established constraints for the red and green blocks (UILabels) to
the outer container.
Set the constraint between them equal to 0. It is more priority than the limitation of the red block to the bottom and green to the top.
Set Lines = 0 for these labels.
Set Autoshrink.
As a result, the font size changes on different devices. But there are still a few problems:
How can I remove too large paddings above and below both labels?
How to make them resize evenly?
Now one of the blocks has an advantage over the other, depending on what constraint to make a higher priority. If you make them equal - it also does not work.
(I would like to do everything through Interface Builder)
Screenshot with differing priorities of constraints
My constraints
Paddings and Attributes
You can take advantage of equal height and equal width constraints.
Follow the below steps to make the UILabel spacing same on iPhone 8 and iPhone 4s. This will help you to make it proportional.
1) To achieve this simply select, your label(red), label (green) and the superview (which I think you're its a cell of UICollectionView)
2) We are interested here in keeping the height proportional.
i.e Red Label (80%) and Green Label (20%)
Currently, all heights are equal to superview height i.e height of RedLabel and Greenlabel is equal to 100% of superview.
But the goal is to make it 80% and 20% for red and green label respectively.
So select Red Label height constraint. Here you set the constraint which says's "height of the red label should be 80% of the superviews height".
Similarly for Green Label, set "height of the greeen label such that it's 20% for the superview's height".
Red Label
Green Label
3) Now complete the x and y axis position constraint, which would be straight forward
a) Red Label Leading = Leading edge of superview
b) Red Label Trailing = Trailing edge of superview
c) Red Label Top = Top edge of superview
d) Red Label Bottom = not required (as it has all the required constraint's to justify it's position i.e height = 0.8 * superview and it is top aligned, ex: super view height is 100 , keep this view top aligned with height = 80 )
e) Green Label Leading = Red Label Leading (you have already set this in point "a" no need to set the constraint again for Green label)
f) Green Label Trailing = Red Label Leading (you have already set this in point "b" no need to set the constraint again for Green label)
g) Green label bottom = bottom of superview
h) Green label top = not required (as it has all the required constraint's to justify it's position i.e height = 0.2 * superview and it is bottom aligned, ex: super view height is 100 , keep this view bottom aligned with height = 20 )
This is the final constraint list and the storyboard preview for iPhone 8 and 4s.
how to fit only the height size of label ...
i have tried this:
servicedes.sizeToFit()
but this will fit both height and width... while i just want to fit the height of it so the text will be at the first line of the label ... my label is a multi lines label ..
this is my label
and this is what i got:
the space by the blue arrow is what i don't want ...
constraints:
how to do this?
Layout constraints aside, you should be able to use .sizeToFit() in a way that it only resizes the label's height by setting the width just before:
label.frame.width = label.frame.width
label.sizeToFit()
This approach won't work when using the constraints you're using right now.
Do the following:
Remove 'Center X' Constraint of the label
Add Trailing constraint(eg. 8) to the label and set relation of Trailing Constraint to 'greater than/equal to'.
Adding Trailing constraint:
Setting Relation:
I want the DecriptionLabel (the Lorem Ipsum one) to have all the text inside it visible. As you can see, it is getting trimmed.
The two buttons should be under everything else, but in the case where DescriptionLabel contains a small text, the buttons should stick to the bottom of the view.
This is why I chose a >= 20 distance between the buttons and DescriptionLabel if it makes any sense.
How can I solve the trimming of the text?
Thanks.
I was originally answering How to make button stick to bottom of scroll view if the content isn't large enough? but since it is marked as duplicate of this one I am posting my answer here. Please try to set your constraints the following way: https://imageshack.com/a/img923/6671/Txzu98.png
The trick is that you set Button.Bottom Equal To ContainerView.Bottom with lower priority (I use 750) than Button.Top Greater Than Or Equal To Label.Bottom (Here I use default 1000)
The Label has to have number of lines set to 0. The height of the button should be set by height constraint (in this case is 50). The Container View Height constraint should be with low priority (in this case 250)
You should run the code to see actual result on device or simulator. Storyboard shows it a bit differently. For the current question:
https://imageshack.com/a/img923/7276/tQeT0h.png The basic idea is the same. Button Down has the same constraints as Button from above answer without Button.Top Greater Than Or Equal To Label.Bottom. There should be fixed vertical constraint between Button Up and Button Down. I am setting Button Up with fixed Height and setting trailing and leading constraint equal to trailing and leading of Button Down. The constraint Button.Top Greater Than Or Equal To Label.Bottom is now Button Up.Top Greater Than Or Equal To Label.Bottom
Have you set numberOfLines for label to 0 (that means autosize the label according to its text length)?
You should add the following constraints:
(following in sudo code)
// Constraints for ScrollView
scrollView.top = ViewController.view.top
scrollView.leading = ViewController.view.leading
scrollView.trailing = ViewController.view.trailing
scrollView.bottom = ViewController.view.bottom
// Constraints for View
view.top = scrollView.top
view.leading = scrollView.leading
view.trailing = scrollView.trailing
view.bottom = scrollView.bottom
// Width of view
view.width = ViewController.view.width
Now you just need to make sure you have layout constraints for each child of the 'view' and it's height will be correct and display the full size of the textview.
Add the following constraint:
scrollview.contentview.height >= safearea.height
This may show an error in interface builder but works in my tests:
To remove the design time error you could set a design time intrinsic content size for the scrollview's contentview (in my case I used the safe area's height of 554):
Another option (without placeholder values in IB) is to create the following constraint...
scrollview.contentview.height = safearea.height
... and change its priority to a value lower than the label's vertical content compression resistancy.
As I am new to iOS. So forgive me if it is duplicate or very basic question.
I am taking one View. Approx below is the size .
x : 5 y : 5
Width : 590 Height : 100
and I set constraint it
Top to superView 5
Trailing to superView 5
Leading to superView 5
Now I have one Label which have dynamic Text and the Text is too large.
And the Label Constraint is below
Top to superView 5
Trailing to superView 5
Leading to superView 5
and when i set the background color of the View the color is not set. If the Text is to Long. So how to set the Height of the View and also set background so that it looks clear.
Code :
public override void ViewDidLoad()
{
base.ViewDidLoad();
lbl_one.Text = "This is a long label which have long text inside the writing. This is a long label which have long text inside the writing. This is a long label which have long text inside the writing. This is a long label which have long text inside the writing";
lbl_one.LineBreakMode = UILineBreakMode.WordWrap;
lbl_one.Lines = 0;
view_main.BackgroundColor = UIColor.Red;
}
If I give fix Height then it look like this .
Output :
1. Give the below constraints to your view, height is according to your need. here I'm giving 80.
2. Change the height relationship.
3. Add aUILabel in your above UIview, and give below constraints.
--> leading, top, bottom, trailing to uiview and height i.e. 80.
4. set height relationship as you do with UIView.
5. Change the property of UILabel , Lines to zero
6. Now enjoy with your constraints.
EDIT: Add bottom constraint to your view instead of height constraint.
I don't see and bottom constraint added to UIView, so the view height will be 0.
If you have added the height constraint to UIView, there is a probability that UILablel might be overlapping the UIView, so you are not able to see the background color.
Set the UIView height constraint this will solve your problem
You can also add height or bottom constraint to your UIView.
I want to implement autolayout to set six square images that are always be in square even if screen size is changed.
I have tried too many variations but fail to do so.
In attached image i share sample view that autolayout will be applied.
You don't need any view wrappers or other funny business here, you can do it purely within IB or AL constraints between each item. The 'trick' is to think about the relationships between each item and to use both constants and multipliers.
Each square here is 1:1 ratio.
The orange square is 2:1 with to the first yellow square, plus 8 for the padding.
The orange square is pinned to the left, the first yellow square is pinned to the right.
All the other yellow squares are relative width to the first one.
Here's the storyboard file too:
https://www.dropbox.com/s/pk8iwj1beamkxtp/SO_Solution-20151215_2.storyboard?dl=0
Based on a comment, I added one wrapper view to make it easy to apply size classes if you want the entire thing to always be visible. (also makes it easier to drop into another storyboard).
Okay, i'll give you an easy way to achieve this, but this is my implementation, and i'm pretty sure there are a lot of implementations easier.
First, create a empty subview, and add the constraints so the view will always be a square in the top left corner:
Trailing Space to superview >= 0
Trailing Space to superview = 0 #750
Top Space to superview = 0
Left Space to superview = 0
Bottom Space to superview = 0 #750
Bottom Space to superview >= 0
Aspect ratio : 1
Now add in this square the square in the top left corner and the topRightView :
// TopLeftView constraints :
Leading Space to superview = 20
Top Space to superview = 20
Aspect ratio : 1
// TopRightView constraints :
Trailing Space to superview = 20
// Contraints between TopRightView and TopLeftView
Align bottom
Align top
Equal Width
Horizontal spacing = 20
You can now set the ratio between the squares, by setting the multiplier value of the "equal width" constraint. Let's use a 1/3 multiplier.
Let's add the bottomLeftView now. In order to not over constraining our view, we don't need to set a multiplier between the square height and this view height. We know the space on the right of the green square is equal to the space below it, so let's use only spacing and alignement constraints.
// BottomRight constraints:
Bottom Space to superview = 20
// Contraints between BottomLeftView and TopLeftView
Align left
Align right
Vertical spacing = 20
The last view to add is the BottomRightView, and alignment constraints will works well :
// Contraints between BottomRightView and BottomLeftView
Align top
Align bottom
// Contraints between BottomRightView and TopRightView
Align left
Align right
Here we are. Now you just need to add squared subviews in top and bottom of TopRightView, and at left and right of BottomLeftView. You can also change the ratio with a single variable, which would not be possible if you had set a ratio constraint between TopLeftView and BottomLeftView.