ios dynamic constraint property for self sizing cell - ios

I have been using constraint for self-sizing and for this case, I don't know how to set up.
If the time label 15-7-2016 maximum y position is higher than imageview, I want that label to determine cell height. If it is not higher, I just want to keep offset of imageview.
What do I need to set up or change programmatically?

Add the below constraints
ImageView: top, leading, trailing, and bottom #750(high priority)
First Label: Top , bottom, leading, trailing
Second Label: Top, leading, trailing, bottom (>= 20)
Hope this will help.

Add the following constraints:
For the ImageView - Top,Leading,a constant
width and height
For the Top Label - Align top to the ImageView,Align Leading to the
label
For the Second Label(which has dynamic Height) - Align Leading to
the top Label,Top Space to top Label,Trailing to the superview and
bottom space to superview.Set the number of lines to 0. Also decrease the content hugging
priority - Vertical of this label to 250.
In the view did load, set myTableView.estimatedRowHeight property to the custom cell row height that you have provided in the storyboard file.

Related

Dynamic label with image in UITableViewCell?

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

My scrollView does not wants to scroll

This is my setup:
I do not know what I am doing wrong. The image view is bigger than the size of the view and of the scroll view. The constrains are set al followed:
Scroll view: equal heights to View * 0,5, equal width to View, center Y and X to View.
View (inside Scroll view): pinned all zero's inside Scroll view, equal heights and width. I also tried instead of equal heights and widths to center X and Y inside Scroll view, but it won't scroll.
How can I let the Scroll view scroll? Thank you.
Add a leading, trailing and top constraint and equal height of UIScrollView to superview with 0.5 multiplier. Now to your contentView (the UIScrollView subview), add a leading, trailing , top and bottom constraint. Also add equal height and width to UIScrollView. Set the height to a priority of 250. Add constraints for UIImageView inside this contentView.
Since the contentView will have a fixed height of low priority equal to the UIScrollView height. This fixed height constraint will break once the UIImageView total height(based on the constraints you add) will get larger than the UIScrollView height and the content will become scrollable. So at the very least you will always have a view half the screen size and become scrollable once the content becomes too large vertically.
You need to give contentSize to scrollview.
ScrollView.contentSize = CGSize(width: 1000, height: 500)
Which constrains have you given to imageview?
set constraint of imageview:
Trailing ,leading,top,bottom - 0 and also give height constraint.

iOS: limit mini height for XIB tableViewCell?

My tableViewCell's bottom equal to that selected UILabel. But if this label have few words cell's height is too small.
So how can i limit cell's mini height equals to that imageView's bottom?
Put your imageview, label Tittle(Fantastic cos) and label description into a view says it main view. Give the main view constraints left, right top, bottom to the cell and height constant as what you what default cell height.
now give contraints to inner components w.r.t. main view:
a) imageview : top, leading, height, width(height is equal to the main view height by constant)
b) label tittle: top wrt main view, leading wrt imageview, trailing wrt main view, and constant height.
c) label description: top wrt label tittle, leading wrt imageview, trailing wrt mainview, bottom wrt main view and height.
Now you contraints given by you defines the default tablecell size i.e. whatever your label description text have this size remains atleast and at most.
Now we have minimun height of the cell, and now we have to change the height of cell as per our text size.
Now, change the both height relation of label descption and main view from equalto(=) to greaterthan or equal(>=).
Now with table view delegates impliment these two methods
a)estimatedHeightForRowAtIndexPath returns (your default tableview height)
b)heightForRowAtIndexPath returns UITableViewAutomaticDimension.
Now run and enjoy with tableview. :)
For More details you can refer : https://www.raywenderlich.com/129059/self-sizing-table-view-cells

Setting top constraint on UIImageView causes UILabel to be fixed height

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];

iOS UIView size

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.)

Resources