How can I set stackView's width according to label's content width?

- 49,246
- 17
- 84
- 119

- 339
- 5
- 13
-
which label? did you set `distribution` property of the stack views to `.fill`? – Ozgur Vatansever Mar 23 '18 at 06:51
-
i need to set both stack-view width dynamically according to label content width size – Pratik Panchal Mar 23 '18 at 06:58
-
I'm not an expert on this but don't you either set a property like width for a view to either a fixed width, dependent on the view it belongs to or possibly a combination of dependent of the view it belongs to and an adjacent view. – Joakim Danielson Mar 23 '18 at 07:11
-
You want label width or height for stack view ? – CodeChanger Mar 23 '18 at 07:15
-
i want to set label height & width dynamically and according Label height width , stack-view height & width also increase. – Pratik Panchal Mar 23 '18 at 07:19
-
I got your point but you are using tableview so we can dynamically set height but what about width its fixed right ? – CodeChanger Mar 23 '18 at 07:24
-
width is also dynamic if content size is increase than label width &height is also increase according to label content size, in short i want to increase height as well as width dynamic for label. – Pratik Panchal Mar 23 '18 at 08:28
-
How did you resolved this? I am also having similar issue – MacDeveloper Mar 09 '22 at 15:26
1 Answers
This can be very easily implemented in a stack view. All you have to do is fix the position of the stack view in the superview by giving contstraints of any two adjacent edges of the stack view(say, leading and top in your case) with respect to its superview.
You don't have to give constraints in between the labels separately like vertical spacing for a vertical stack or horizontal spacing constraint for a horizontal stack(as long as you don't want to override the basic behaviour of a stack view). All this can be easily managed using the distribution and space properties of the stack view (you can find these in the attributes inspector of stack view)
I recommend going through the Apple's doc on UIStackView, particularly the section "Common Stack View Layouts". In "Define the position only" subsection it states,
You can define the stack view’s position by pinning two of its adjacent edges to its superview. In this case, the stack view’s size grows freely in both dimensions, based on its arranged views. This approach is particularly useful when you want the stack view’s content to appear at its intrinsic content size, and you want to arrange other user interface elements relative to the stack view.
Secondly, since you are using the Stack view inside a Table View, if you are changing the label's content dynamically, after loading the Table View. Then you should call beginUpdates and endUpdates of the TableView in order to see the table size grow/shrink/adjust according to the adjusted Stack view's height/width.
If you still face any issues. Please provide more details regarding the UI you want to achieve(expected) and how it's behaving currently. That'll help in providing a more precise answer.
Hope it helps! :)

- 51
- 6