I have a collectionviewcell with a button and a label like this:
The button is a 100x100 square, the label is a 100x40 rectangle. The cell is adjusted to 120x140.
So the constraints I have added are:
Button:
aspect ratio 1:1
top to superview space 0
bottom to label space 0
center horizontally in respect with cell
width = 100
LABEL
same width as button
center horizontally in respect to button
height 40
top space to button 0
bottom space to cell 0
I see constrain errors everywhere.
I have tried to embed these two views on a stack. It craps everything.
Then I have tried to embed the stack into a view, same problem.
This defies any logic.
Can you guys tell me how in the name of heaven I constrain these elements? All I want is both elements centered horizontally, the button on top, the label on the bottom, both with the same width of 100, the button squared and the label with a hight of 40. Both in a cell of 120x140.
Thanks
This is how you should set your constraints using a stack view in a cell to accomplish what you need: (cell width = 120, height = 140)
Related
I have a UITableView filled with one type of custom UITableViewCell. Each cell contains a title (UILabel), subtitle (UILabel), and thumbnail (UIImageView), as shown below.
The widths of all three subviews are fixed, but only the subtitle label has a fixed height. The title label's height is dynamic according to the amount of text. I have set the title label's numberOfLines to 0 to reflect this.
My objective: The height of the cell should be determined by the height of the title label. In other words, the cell height should be the sum of the two labels' heights plus the three vertical padding gaps. The image height should be set to the resulting height of the cell.
My current implementation: I have set the following autolayout constraints:
Thumbnail is pegged to superview on top, left and bottom
Thumbnails width is proportional to superview width by a multiple of 0.2
Thumbnail is pegged on its right side to title and subtitle (trailing space = 8)
Title is pegged to superview on top and right (trailing space = 8)
Title is pegged on its bottom to subtitle (trailing space = 8)
Title height is greater than or equal to 24
Subtitle is pegged to superview on right and bottom (trailing space = 8)
Subtitle height is equal to 20
Additionally:
Title numberOfLines = 0
Thumbnail contentMode = Aspect Fill
Thumbnail clipToBounds = true
The problem: The image height does not respect the height of the labels, so the height of the cell is set to the full height of the image and the title label is stretched vertically. This is understandable given the constraints I have used, but is not what I want to happen.
My question: How do I constrain the UIImageView's height to respect the cell height (determined by dynamic label height), instead of having the cell height respect the UIImageView height?
My Environment:
Xcode 10
iOS 11 & 12
Swift 4.2
Try setting thumbnail vertical compression resistance lower than title vertical hugging priority. In code it'll look like this:
thumbnail.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
titleLabel.setContentHuggingPriority(.required, for: .vertical)
I'll explain the vertical constraints to make this possible.
Pin the top of the label to the contentView with enough padding so that image view stays inside it.
Pin the top of the subtitleLabel to the bottom of the titleLabel.
Pin the bottom of the subtitle similarly to the bottom of the contentView, so that the image view stays inside it.
Your cell now has a height based on the above constraints. Now you just have to make sure you place the image view inside this height.
Pin the top of the thumbnail to the top of the label, bottom to the bottom of the subtitle label.
Example:
titleLabel.top -> contentView.top + 10
subtitleLabel.top -> titleLabel.bottom + 5
subtitleLabel.bottom -> contentView.bottom - 10
imageView.top -> titleLabel.top - 5
imageView.bottom -> subtitleLabel.bottom + 5
We have two(first label dynamic height) labels and one in UITableview and our image size static assume that 120*70
We are setting image bottom constraint to cell superview but when Label content height is more than image height, second label going inside cell automatically.
We are using UITableViewAutomaticDimension
Here we confused to set constraint to labels, How to proportionally set heights to both labels and image.
If we try to bottom constraint to label and fixed height to and width to image, then white space coming at bottom of cell.
My cell design looks like below image:
Check my output with below constraints
Output:
Constraint image:
You can do this easily by setting a bottom constraints of greater-than-or-equal to both the bottom label and the image view.
First, make sure you are using:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100
so the cells / rows will auto-size to fit the contents.
For the cell prototype, I have (background colors so we can see the frames):
Multi-Line label constrained Top 8 and Leading 8 (from the superview, not using margins)
Image View constrained Top 8 and Trailing 0 (from the superview, not using margins), and Width 119 Height 87 (based on your posted image)
1-Line Label constrained Leading and Trailing to Multi-Line label, and Top 4 to Multi-Line label
Multi-Line label is also constrained Trailing 8 to the image view
Now the key is the next two constraints...
constrain Bottom of 1-Line Label at >= 8 to Bottom of superview
constrain Bottom of Image View also at >= 8 to Bottom of superview
Now, the cell height will auto-expand to fit the taller elements:
and, how it looks without the colored backgrounds:
The full project can be found here: https://github.com/DonMag/AnotherExpandingCell
I'm having this weird issue with my constraints which causes the UILabel (Caption Label) to be a fixed height instead of dynamically changing height depending on the text.
I have a view (Vertical View) with a top constraint on the label above it. The Vertical View contains a view (called View) which I'm using as a divider that is centered from top to bottom with a width of 1. On the left of the divider is a UIImageView (Left Image View) with constraints leading, top, bottom equal to superview and trailing equal to View. I want to do the exact same thing to the UIImageView on the right of the divider but here is where my issue comes up.
If I use a fixed height as seen below, the UILabel above Vertical View dynamically changes its height like I want but this is obviously not how I want the UIImageView on the right to appear. I want it to be similar to the UIImageView on the left of the divider with equal height and width.
If I set the top constraint of the UIImageView on the right to the superview Vertical View, similar to the UIImageView on the left of the divider, the UILabel above Vertical View doesn't dynamically change height anymore. The UILabel now has a fixed height which I believe comes from the fact that UILabel has a height of >= 14.
How can I properly set the constraints so that I can have both UIImageViews next to each other with equal and height contained within the Vertical View and still have the UILabel above Vertical View dynamically change height depending on the text that I set the UILabel to?
On the RightImageView, you first need to get rid of the "Height = 50" constraint. This is what is causing it to be small.
Next, if that alone doesn't fix you, can you try setting the following constraints instead of using the superview for the constrains (instead make it mirror the LeftImageView):
Left: Leading spacing to divider view
Top: Align top edges to LeftImageView
Right: Horizontal space to superview (your vertical container view)
Bottom: Align bottom edges to LeftImageView
This should allow the views to remain the same height and width (assuming your distances between left/right edge of vertical container view are the same, and the distances between divider are the same).
Now, ensure the size constraint for width of the divider is set to 1 and not >= 1. Also, ensure the vertical container view has a Compression lower than the Label.
One final note--your screenshot shows the result that IB is showing you (with the dotted yellow box) on the LeftImageView. One you update your constraints correctly, this yellow box should go away.
Regarding the UILabel - if you want this to grow dynamically, you need to do the following:
myUILabel.numberOfLines = 0;
myUILabel.text = #"Enter large amount of text here";
[myUILabel sizeToFit];
I have a Controller with 4UILabels that are constant height throught all the iPhones, below the lastUILabel there is aUIView with a page controller(with aTableView inside that page controller).
What I want is that theUIView take all the height that he cant :
example: Screen of 600 height
4UILABELS = 200 height
TableView = it should get 400 height
Screen of 800 height
4UILABELS = 200 height
TableView = it should get 600 height
I need 1 constraint more to set the height of the view,What I have defined is :
EqualWidth constraints to superView
LeadingSpace to SuperView
Top Space to super VIew
The following constraints should align everything for you so that the UILabels stack vertically, each have height 50, and stretch to the edges. The UIView will take up the remaining space regardless of the screen size.
All UILabels
Pin leading edge to superview
Pin trailing edge to superview
Add height constraint set to 50.0
UILabel 1:
Pin top edge to superview top
UILabel 2:
Pin top edge to UILabel 1 bottom
UILabel 3:
Pin top edge to UILabel 2 bottom
UILabel 4:
Pin top edge to UILabel 3 bottom
UIView
Pin leading edge to superview
Pin trailing edge to superview
Pin top edge to UILabel 4 bottom
Pin bottom edge to superview
UITableView
Pin leading, trailing, top and bottom to its container view so it fills the area.
I assume that you are creating this UIViewController in a Storyboard.
You should be able to put all the labels in a container view which you give the constant height of 200 points, as you specified. Make a vertical constraint from "Top layout Guide" to said container view, maybe with the value 0. Drag an UITableView and place it below the container view. Dont give it any constraint regarding height. Make a vertical spacing between the (bottom of) container view and the (top of the) UITableView with value 0 and a vertical spacing between the (bottom of the) UITableView and the Bottom Layout Guide with value 0.
So it will be (V:0 means vertical spacing, constant 0):
Top Layout Guide
V:0
Container View - Height: 200
V:0
TableView (dynamic height)
V:0
Bottom Layout Guide
This should work.
(And then of course you need to create constraints regarding width and also internal constraints for each UILabel inside the container view. Let me know if you need help with that.)
Inside of a uitableviewcell I have an image view and a label side by side. I'm using systemlayoutfittingsize + a sizing cell to drive the table view cell height via autolayout for ios7/8. I'm also using setting up these constraints programatically (no storyboard answers please). Two possible scenarios...
Scenario 1: Label height is intrinsically smaller than image height
Desired result: Size of the cell expands so that the image (set explicitly to 100 width, 75 height) is centered vertically in the bounding area, and the label's top edge is aligned with the images top edge.
Scenario 2: Label height is intrinsically greater than the image height
Desired result: Size of the cell expands so that the label is centered vertically in the cell. The image's top edge aligns with the label's top edge.
So the constraints needed for this appear to be
For the image view:
1. Pin width = 100
2. Pin height = 75
3. Pin leading space to superview = 10
4. Pin top space to superview = 10
For the label:
1. Pin top space to superview = 10
2. Pin bottom space to superview = 10
3. Pin width = 200
4. Pin trailing to superview = 10
5. Pin horizontal spacing = 10 (between label and image view)
Then in the code all we have to worry about is the "heightForRow" UITableView delegate method. Since we did not set a static height for the label and we pinned it to the top and bottom of the superview/cell it will change size dependent on the height of the UITableView cell. We have to calculate the "numberOfLines" for the dynamic length of the text of the label. There are many links out there that can help you with this - How to calculate UILabel height dynamically?