In a XIB
file I have a CustomView
related to a class with the same name.
The view content is quite simple.
There are a vertical stackview and inside the stackview there are two labels. The stackview is vertically aligned to the view that contains it:
The first label (title) contains a static text (always a few characters). The second label (subtitle) can have variable length text.
I add this CustomView
with other similar views as a subview of a Content View
, as it was a “row”.
for i in 0...aViews.count {
var v = CustomView(frame: CGRect(x: 0, y: _y, width: 320, height: 100))
v.labelText = "my long label text ..."
contentView.addSubview(v)
// ...
}
As you can imagine, the title label should have a fixed position (top and left), that is, there cannot be a row with the title starting at 10 points and another at 14.
I must admit I naively thought the position of the label would have been automatically managed by the same fact that I aligned vertically the stackview. I was wrong and I noticed no problem at all, until they told me the second label, the subtitle, could contain more than one line.
I added lines to the label directly in the storyboard
, and found out that:
the container of the stackview doesn’t change its
height
based on theheight
of its content;the position of the “fixed” elements is maybe vertically centered but not fixed;
What I need is the following:
- the labels should be “grouped” and aligned vertically: there should be the same amount of space from the top of the first label to the upper border of the container and from the bottom of the second label to the bottom border;
- when the second label has to display more than one line, the container view should change its height accordingly;
Is this possible? How should I design my view?