When a label and a button share a horizontal UIStackView and the row is too narrow, one of them has to shrink. Both start with a horizontal compression resistance of 750, so Auto Layout has no rule for which one gives way, and the button can end up squeezed to nothing. Give the button one point more, .defaultHigh + 1 (751), and the label is cut short instead. You don't need 1000: Apple's Auto Layout Guide advises against required priorities here.
SWIFT TIPS · UIKIT · AUTO LAYOUT
First posted on November 13, 2019, as "Set priority button between label in UIStackView". Rewritten on November 18, 2026. The layout tests were run on a Mac as a Mac Catalyst program, which runs UIKit on macOS (not an iPhone or the simulator), with Swift 6.2.3 (Xcode 26.2) on macOS 26.0.1. The cell was type-checked against the iOS 26.2 SDK in Swift 5 and Swift 6 modes with iOS 15 and iOS 26.2 as deployment targets, but not run on a device.
In 2019 I had a table cell with a long label and a small button side by side in a horizontal stack view. The button's title didn't show: the label took the space, as the screenshot below shows. The fix in the post was one setting in the Size inspector: raise the button's horizontal Content Compression Resistance Priority from 750 to 1000 and leave the label at 750.
That fix works. This rewrite explains why the bug happens at all, shows that the result depended on something you wouldn't expect, and uses a smaller number.
Who gets squeezed when both are at 750
Compression resistance is how strongly a view refuses to be made narrower than its content. UIKit gives labels and buttons the same default, 750. When the row is too narrow for both, Auto Layout must break one of the two "don't squeeze me" rules, and since they have the same priority, nothing says which.
I built the row in code, a long label and a "Details" button in a 320-point-wide stack view, and measured the widths after layout:
1. Long label, 320 pt wide (both resistances 750):
label first: label 231 pt, button 49 pt
button first: label 280 pt, button 0 pt
2. Long label, button resistance 751:
label first: label 231 pt, button 49 pt
button first: label 231 pt, button 49 pt
3. Row only 80 pt wide, too narrow even for the button alone:
button 751: label 0 pt, button 40 pt
button 1000: label 0 pt, button 40 pt
- Equal priorities leave the loser undefined. With both at 750, the label was squeezed when it came first in the stack, and the button was squeezed to 0 points when it came first. In this test the result followed the order, but the 2019 cell had the label first and still lost its button, so you can't count on order either. Pick the winner yourself.
- 751 settles it. With the button one point higher, it kept its full 49 points in both orders, and the label was the one cut short.
- Priorities can't make room that isn't there. In a row only 80 points wide, the button got 40 points whether its priority was 751 or 1000. A higher number doesn't help when even the button alone doesn't fit.
A cell where the button always keeps its title
import UIKit
/// A table cell with a text label and a button in one horizontal row.
/// When the row is too narrow, the label is cut short and the button keeps its full title.
final class ReviewCell: UITableViewCell {
static let reuseID = "ReviewCell"
let titleLabel = UILabel()
let detailsButton = UIButton(configuration: .filled())
var onDetails: (() -> Void)?
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
titleLabel.font = .preferredFont(forTextStyle: .body)
titleLabel.adjustsFontForContentSizeCategory = true
titleLabel.lineBreakMode = .byTruncatingTail
detailsButton.configuration?.title = "Details"
detailsButton.addAction(UIAction { [weak self] _ in self?.onDetails?() }, for: .touchUpInside)
// Both views start at 750. One point more decides which one keeps its size.
detailsButton.setContentCompressionResistancePriority(.defaultHigh + 1, for: .horizontal)
// Spare width goes to the label, not the button.
detailsButton.setContentHuggingPriority(.defaultLow + 1, for: .horizontal)
let row = UIStackView(arrangedSubviews: [titleLabel, detailsButton])
row.axis = .horizontal
row.alignment = .center
row.spacing = 8
row.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(row)
NSLayoutConstraint.activate([
row.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
row.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor),
row.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
row.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor),
])
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override func prepareForReuse() {
super.prepareForReuse()
onDetails = nil
}
}
In the same Catalyst program, with the 2019-style long text, the cell gave the button its full width at three common iPhone widths:
ReviewCell 320 pt wide: label 217 pt, button 78 pt (needs 78)
ReviewCell 375 pt wide: label 272 pt, button 78 pt (needs 78)
ReviewCell 430 pt wide: label 327 pt, button 78 pt (needs 78)
How it works
- Compression resistance decides who shrinks.
setContentCompressionResistancePriority(.defaultHigh + 1, for: .horizontal)sets 751 on the button. When the row is too narrow, Auto Layout breaks the weaker rule, the label's 750, so the label gets narrower and shows "..." at the end because of.byTruncatingTail. - Content hugging decides who grows. Hugging is the opposite rule: how strongly a view refuses to be made wider than its content. Both views default to 250 here. Setting the button to 251 means spare width goes to the label, so a wide screen doesn't stretch the button.
- Why not 1000. Apple's Auto Layout Guide says to avoid giving views required hugging or compression resistance priorities, because it's usually better for a view to be the wrong size than to cause a conflict. It suggests 999 if a view should always keep its size. Here 751 is enough, because it only has to beat the label.
- Layout margins. The stack view is pinned to the content view's
layoutMarginsGuide, so the row keeps the system's side margins and lines up with other cells.
In Interface Builder
The 2019 fix was done in the Size inspector, and it works the same way today: select the button, find Content Compression Resistance Priority, and set Horizontal to 751 instead of 1000. While you're there, set Content Hugging Priority Horizontal to 251 so the button doesn't stretch on wide screens. The label can stay at the defaults.
A quick check for your project
- Find horizontal stack views that hold a label and a button or a second label. If both have the same horizontal compression resistance, the result depends on order and text length. Pick a winner.
- Search the storyboard source for
horizontalCompressionResistancePriority="1000". In most rows 751 does the same job without risking a conflict. - Test the row with the longest real text you have and the largest Dynamic Type size. That's when the squeeze shows up.
What the 2019 version said
The original post showed the result, the bug and the inspector setting. In the screenshot from before the fix, each button shows only "..." because its title didn't fit. In the GIF from after the fix, the buttons read "isBlah". The inspector shows Horizontal Compression Resistance set to 1000. The post also ended with a link to the contact page of the blog's old address, which I left out.



Keep reading on JR Devhub
Comments