2

I want to achieve following UI:

enter image description here

This UI contains label (which is bold on top), stackview (inside blue rectangle), view (gray line) and two labels below of line.

As you see, I have separate view inside stackview. Here is how its constraints are set:

titleLabel.snp.makeConstraints { make in
        make.leading.equalToSuperview().offset(16)
        make.top.equalToSuperview()
        make.bottom.equalToSuperview()
    }

    priceLabel.snp.makeConstraints { make in
        make.trailing.equalToSuperview().offset(-16)
        make.top.equalTo(titleLabel.snp.top)
        make.leading.equalTo(titleLabel.snp.trailing).offset(16)
    }

My goal is to align top of the titleLabel to top of the priceLabel. It seems everything is working, but here is the screenshot of this UI with different text:

enter image description here

As you see, my stackview doesn't give space for bold label. I don't want it to happen. This UI is shown inside bottom sheet with intrinsic layout. I think the problem is that my priceLabel doesn't have bottom constraint. Let's try to give it:

priceLabel.snp.makeConstraints { make in
        make.trailing.equalToSuperview().offset(-16)
        make.top.equalTo(titleLabel.snp.top)
        make.leading.equalTo(titleLabel.snp.trailing).offset(16)
        make.bottom.equalToSuperview()
    }

Constraints of titleLabel wasn't changed. Here is the result:

enter image description here

It seems everything is ok, but my first titleLabel is too small, even it has numberOfLines set to 0. The another problem is that my priceLabel's top is not aligned with titleLabel's top. I don't want it to happen.

After that, I remembered that UIStackView in UIKit has firstBaseline alignment. Then, I decided to put titleLabel and priceLabel inside horizontal stackview with firstBaseline alignment and fill distribution:

private lazy var stackView: UIStackView = {
    let stackView = UIStackView()
    stackView.distribution = .fill
    stackView.alignment = .firstBaseline
    stackView.axis = .horizontal
    stackView.spacing = 16
    return stackView
}()


 private func setup() {
    stackView.addArrangedSubview(titleLabel)
    stackView.addArrangedSubview(priceLabel)
    addSubview(stackView)
    stackView.snp.makeConstraints { make in
        make.leading.equalToSuperview().offset(16)
        make.trailing.equalToSuperview().offset(-16)
        make.bottom.top.equalToSuperview()
    }
}

And here is the result:

enter image description here

It is totally ruined. The space between my bold label and stackview is too big. Possibly, the reason for that is bold label's hugging priority.

Final words

As you see, problem is in alignment of labels inside stackview. There are dozens of ways of aligning top of uilabels but all of them give different results. I don't know why. It seems that I don't know something from Auto Layout. So, how to achieve the first screenshot's result?

neo
  • 1,314
  • 2
  • 14
  • 34
  • It's not quite clear what you're doing... Is this a subview? Or is it a view controller that you're presenting? – DonMag Aug 31 '20 at 18:56
  • view controller that is presented using FloatingPanel library – neo Sep 01 '20 at 07:45
  • It's difficult to offer help, without knowing what you're really doing. I grabbed the SCENEE/FloatingPanel repo from GitHub (I assume that's what you're referring to)... if you can show a simple example of what you're trying (see [mre]), it will be much easier to help. – DonMag Sep 01 '20 at 14:34

1 Answers1

1

What you have to do is set a height constraint to your bold label. That way it doesn't shrink.

label.heightAnchor.constraint(equalToConstant: 30).isActive = true
Tad
  • 889
  • 9
  • 23