I have a button and I want it to be as small as possible so it just encloses the text but if the button is too big keep it constrained by the size of the containing view inset by 2 standard units.
This is my code now:
backButton.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.top.equalTo(containerView.snp.bottom).offset(2.su)
make.trailing.leading.lessThanOrEqualToSuperview().inset(2.su)
}
The problem with it is that it sizes immediately to take up the space in trailing
and leading
constraints. Is there a way for it to take less space?
You need to set leading
to be greaterThanOrEqualToSuperview
or trailing
to be lessThanOrEqualToSuperview
. Since you set centerX
to center, you don't need both.
And when you set them as lessThanOrEqualToSuperview
, trailing
works fine, but leading
interferes with it.
backButton.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.top.equalTo(containerView.snp.bottom).offset(2.su)
make.trailing.lessThanOrEqualToSuperview().inset(2.su)
}