Expand UIView width when label inside has more text - ios

I have 2 UIViews with labels inside. The UIView should expand depending on the content in the label. Following is the layout:
Both of the above views can expand depending on the content in the label. I have tried to set layout with autolayout but the width is always fixed. Following are my constraints for second view:
First view constraints:

It looks like your first view is missing a leading constraint to the label inside.

Related

How to make auto resizing label inside scrollview?

I am using a view inside a scrollview and inside the view, there are two labels with fixed width. and I want to make these labels auto-resizing in respect of height. For this I have given the height of the view >= but it's breaking the constraints. How can I achieve auto resizing content within scrollview.
While using scrollview it's mandatory to give the height of the elements inside scrollview. But if we want to make an element self height adjusting then we can give the minimum height with the priority 250
First, you need a content view inside the scroll that has constraints with the controller's view (equally both width and height) and top, leading, trailing, bottom with scroll view.
Then, add any subviews inside that content view. In your case is the label's bound view.
Something like:
The red one is the content of your scroll view
The gray one is bound label view
Just embed all these labels in stack view. And that's it.

UIView dynamic height inside UIStackView

I currently have a Stack View setup with a bunch of different objects inside of it, and I'm trying to add a view to it.
The view will have a dynamic number of labels added inside of it programmatically at run-time.
When I put the view inside of the stack view, I get errors with the constraints, which get fixed by adding a height constraint.
However, I don't know the height of the view initially so I can't constrain this.
Is there any way to have a UIView inside of a StackView auto-resize based on it's content size? I thought this would be automatic, but when I don't put a height constraint, the view doesn't show up
You don't have to put a content view inside the stackview to add the elements to it , you need to hook the stackview with only ( leading , trailing , top) constraints to the superView
then add elements with
stack.addArrangedSubview(view)
the most important thing is that the elements you add must have an intrinstic content-size like label and button , and if not then add a height with
someView.heightAnchor.constraint(equalToConstant:<#setValue#>).isActive = true
with distribution set to Fill
Steps to add custom UIView with dynamic height inside UIStackView
Add UIStackView to superview in IB and add top, bottom, leading and trailing constraints.
In case IB is throwing any error for constraint, just set intrinsic size for UIStackView as PlaceHolder with some dummy size. This will fix UIStackView position in IB but this will automatically change at run time as per auto layout.
Create instance of CustomUIView as below
Bundle.main.loadNibNamed(nibName, owner: self, options: nil).first as! CustomUIView
Add UIView to UIStackView as below.
stackView.addArrangedSubview(customView)
This will add custom UIView to UIStackView and it will expand/collapse as per xib content and constraints. Just make sure your xib has proper constraints as per auto layout guidelines.

UIScrollView height while adding content programmatically in UIStackView

I am trying to make a view containing an UIScrollView. This UIScrollView contains 3 UIViews. The last one contains another UIStackView I wanted to fill at runtime.
Here an image of the storyboard :
But when I add content at runtime in the second UIStackView, the ScrollView's height remains the same.
The second UIStackView is defined as the first one with:
Axis : vertical
Alignment : Fill
Distribution : Equal Spacing
Then I use :
mStackView.addArrangedSubview(matProgress)
The result below :
What's the way to have the bottom view and the ScrollView stretch to fit the content.
The layout can be done with auto-layout and constraints.
The key points are:
do not set height constraints on either StackView
set the "Main" StackView Distribution to Equal Spacing
set the "Main" StackView Leading, Trailing, Top and Bottom constraints to its Superview (which is the ScrollView)
you do need to also set a width constraint on the "Main" StackView to control the horizontal contentSize
The only quirk will be on startup. If you have NO content in the bottom / inner StackView, it will still "exist" in the Main StackView and take up space. There is a trick to get around that, but it must be done in code.
You can see a working example here: https://github.com/DonMag/StackyScrolly
If you have UIScrollView and the child views with auto-layout. Setting programmatically the height of the scrollview will stretch the inside views to comply with the auto-layout.
So, you get by code the height of the third view and set the UIScrollView height to view1.height + view2.height + expectedThirdViewHeight.

Center multiple UILabels on a line

I want to horizontally center multiple UILabels - as a group - on a line in Interface Builder. One the straight view controller i could not figure out how to do this. I read comments about place the UILabels in a View and then centering the view in the view controller. When I tried this, overtime I said to update frames in the interface builder, the View would be resized down to nothing. (i.e. its height and width would be set to 0 by IB). I need to know how to get this to work in interface builder.
An example of a line containing multiple labels I want horizontally centered is below. The braces simply indicate the start and end of each label and are not art of the text. The <- 6 -> is meant to indicate the trailing space from label 1 to label 2 is 6
Label 1 Label 2
[Rating: 0.0]<- 6 ->[Distance: 125.34 Kilometers]
Any suggestions would be greatly appreciated.
Here is an EXACT example of what I have done and it does not work:
Create a new view controller in IB
Place a page label at the top of the page centered horizontally and aligned to the top of the layout guide.
Add a view with the following constraints on the page:
Now add two labels to the view with the following constraints:
Once this is done I get the following error and updating the frames will cause it to be give a height and width of 0
Here is what the page looks like in IB:
I need the height and width of the view to size automatically so that I have put a multiline label in the view and have all the contents of the view treated as a group and centered on the page.
Any suggestions would be greatly appreciated.
Here is a snapshot of a working set of all the constraints on an abbreviated layout that produces an always-centered view that automatically resizes with the child views (i.e., as the content of any label changes, the view grows or shrinks around it).
Important to your solution, the view has neither a height nor width constraint; its size is constrained entirely by its descendant constraints.
The key is that every component has a direct or indirect constraint from which its size and position are specified or can be inferred. For example, in order for the view to infer its width, the child labels must have a leading space constraint on the first label, a trailing space constraint on the last label, and a horizontal space constraint between interior labels. Those constraints plus the contents of the labels allow the width of the view to be inferred ... and force the view to dynamically conform to that width.
The same applies for the view's height. For example, you can specify the top and bottom space of just one label, or all of them. If just one, the remaining labels can be vertically aligned with it (see "Align Center Y to: Label1" in the screenshot).

Setting UILabel to width of screen and height to fit contained text

I am trying to layout a detail view of my application. The view has a title, some information, and a description. I would like to set the width of my title label and the description label to be the width of the screen and the height of those labels to accommodate the contents of the labels. I also want the entire view to scroll if the height of the content is too large to fit on the screen.
I currently have a xib with the labels inside a view, with autolayout constraints to position the objects and set the width of the title label and description label to the size of the containing view. That view is inside a UIScrollView but does not have any constraints on it. I set the labels to accommodate multiple lines in the attributes inspector. I set the width of the view with the content and the scroll view content size and frame in my viewDidLoad method.
Currently I cannot get the labels to be the width of the screen and the height to automatically accommodate the text. Am I on the right path or do I need to try a different method?
To accommodate the height of the text inside, use 0 as the number of lines in the UILabel. That will automatically make them grow according to the text inside. You will also need to add a constraint from the last element inside your UIScrollView to the bottom of the scroll view, to avoid ambiguous content size.
To get the labels to be the width of the screen, it should be a straightforward constraint from the label to it's container. However, if the label is within the scroll view, you might have to constrain the label to have the same width as the scroll view, because constraining the label to the leading and trailing edges of the scroll view will constrain it to the scrollview's content view, which grows according to its contained views.

Resources