I'm having layout issues with my horizontal UIStackView
in a custom UITableViewCell
when showing/hiding arrangedSubviews
when the horizontalSizeClass
changes.
My stack view contains a number of subviews, each of which, depending on cell configuration and size class, are either hidden
or shown. The UIStackView
is designed to handle arranging the shown views, but upon rotation, layout issues arise.
Issues:
- Sometimes, the appropriate subviews are either not shown or not hidden when they ought to be.
- Sometimes, the subviews are inappropriately laid out, not filling the stack view's width.
Attempts:
I've attempted a number of things to address the layout:
- Overriding
viewWillTransitionToSize:transitionCoordinator
to reload the table and/or force layout - Overriding
viewWillTransitionToTraitCollection:withTransitionCoordinator
to reload the table and/or force layout - Overriding
layoutSubviews
to re-configure the stack view'sarrangedSubviews
- Calling
[self setNeedsLayout]
,[self layoutIfNeeded]
after configuring the cell - Forcing layout in other places
- Changing subview layout constraint priorities to 999
- Limiting the
UILabel
s to 1 line, and setting apreferredMaxLayoutWidth
- Adjusting the
contentCompressionResistance
andcontentHuggingPriority
on views - Using a static value for
rowHeight
instead ofUITableViewAutomaticDimension
- Etc.
Nothing appears to fix the issues.
Furthermore, even when cells are scrolled off/on screen, prepared for reuse, and re-configured, issues either persist, go away, or new issues are introduced, despite the fact that I'm properly resetting the cell on prepareForReuse
.
Sample Project
I've created a sample project to illustrate the layout issues. At this point, I'm unsure if UIStackView
is buggy or if I'm misusing it.
Sample Project: https://github.com/bradgmueller/StackViewTest
The sample project uses a custom UITableViewCell
with views configured in the xib. Row objects are generated with different configurations to illustrate the dynamic layouts the cell should adopt:
- Indented / not indented
- Showing separator or not
- Showing / hiding a "like" button
- Showing / hiding a "Share" button
- Showing / hiding an Info button, where one info button exists for
UIUserInterfaceSizeClassCompact
and another forUIUserInterfaceSizeClassRegular
A text label exists with text indicating which of the views ought to be displayed, to help illustrate when views are inappropriately shown/hidden. Also, a red background view exists behind the UIStackView
to illustrate when the stack view is failing to Fill the width.
Screenshots:
After rotating - issues marked with red "X"
I'd appreciate any insight, thanks in advance!!