I want to create custom view like below.
As you see, it consists from title and price labels. Title can have million number of lines, but its top edge should be aligned with price label. It seems simple design, but it has hundreds of solution. I tried every of them, but my title label is not growing, by having dots at the end (numberOfLines = 0 doesn't help). Here is how I approached to create such a design:
I created titleLabel with top, leading, trailing to price label, bottom constraints. Also, I created price label with top and trailing constraints only in order to align their top edges. I assigned compression resistance and hugging priority to price label, because it is more important and should not be ruined. Here is code if you want:
addSubview(titleLabel) addSubview(priceLabel) titleLabel.snp.makeConstraints { make in make.leading.equalToSuperview().offset(16) make.trailing.lessThanOrEqualTo(priceLabel.snp.leading).offset(-8) make.top.equalToSuperview() make.bottom.equalToSuperview() } priceLabel.snp.makeConstraints { make in make.trailing.equalToSuperview().offset(-16) make.top.equalTo(titleLabel.snp.top) }
I created separate custom view, because I want to use it inside StackView(spacing 8, distribution fill, vertical). Result of this approach: title label's is not growing. It has only one line with dots at the end, if it has big text.
- Second approach was to create stackView (horizontally, spacing 8, distribution fill, alignment top). I set alignment top in order to align top edges of the labels. The result was the as in approach #1.
How to solve this problem? Where am I wrong? It seems I don't see something core in Auto Layout theory here.
During development of your layout, it can be very helpful to use contrasting colors for element backgrounds... makes it really easy to see what's happening with their frames.
Give this a try...
Custom view class
Example view controller class - adds another label constrained 4-pts from the bottom of the custom view so we can see everything working:
Result (Red is custom view with title and price labels, Blue is a label added and constrained below the custom view):