This is just a sample project I’m using to test some AutoLayout stuff.
I’m using Follow Readable Width but then the label is still going all the way to the edges. I’ve tried a number of variations but nothing worked…
When I inspect it from the viewDebugger, I see no trace of setting the Readable Width.
Anyone know what am I doing wrong?
EDIT:
Followed instructions based on provided answer. I did the following:
- set ‘Follow Readable Width’ on the superview
- set constraints between the label and its superview’s margins
- set my label’s number of lines to 0
But it’s still not working.
2
Answers
Apple’s docs on this are, perhaps, somewhat lacking…
Follow Readable Width uses the readableContentGuide to determine the "comfortable readable width" (based, I’m sure, on plenty of research).
However, as may not be entirely intuitive, it applies to a view’s subviews — not to the view itself.
So, look at this example. Two plain view controllers, each with a standard
UILabel
. Number of lines: 0, system font at 22.0.Note they are constrained to the
superview's margins
(Top: 80, Leading: 0, Trailing: 0) — NOT to the safe-area!The only difference is that the
(root) View
of the VC on the right has Follow Readable Width checked (True).The frame of each label (using iPhone 12 Pro Max) is
388 x 144
.If we rotate them to Landscape Orientation (and move the right-hand VC below):
We can see the label has a different frame:
806 x 105.33
664 x 131.33
And, to clarify what’s being set:
Here’s another example…
(root) View
Safe Area (Top: 80, Leading: 0, Trailing: 0)Neither VC has Follow Readable Width checked on the
(root) View
GreenView in the right-hand / bottom VC does have Follow Readable Width checked (True):
Edit – here’s the Storyboard source for inspection (includes both examples):
Edit 2
I put an example project here: https://github.com/DonMag/FollowReadableWidth
Note, though, that I have found this to be rather quirky (maybe buggy). See the notes in the example.
I finally found something that works dependably for Readable width. This article shows "Constrains to margins" checked. I’ve found if I delete the tableView’s original leading and trailing constraints and replace them with ones made in the "Add New Constraints" control that includes "Constrain to margins" (checked) then the tableView respects the "Follow readable width" (checked) of the root view.