iOS 9 – Xcode: “Follows Readable Width” programmatically

i0S Swift Issue

Question or problem in the Swift programming language:

In iOS 9, there is an option in the Xcode SIZE inspector called “Follows Readable Width” which will place padding on the left/right sides of a view so that it won’t stretch out so far (especially on iPads) in order to make the content easier to read.

(You can see an example of this on the Twitter app when placed in landscape on iPhone 6/6s plus.)

Anyway, I cannot seem to find how to do this programmatically. In the Xcode SIZE inspector, it’s just a checkbox you set. It’s obviously a boolean value you can set on any UIView or anything that inherits from it. I just cannot seem to find anything close to it.

Anyone ever set this programmatically? Please see the attached screenshot.

Oh, I can find the ‘Preserve Superview Margins’ just fine, just not the ‘Follows Readable Width’ property.

How to solve the problem:

The “Follow Readable Width” option you can set in the Interface Builder is handled by code in two different ways:

1) UITableViews

There is a property for UITableView to trigger the readable width:

if #available(iOS 9, *) {
    tableView.cellLayoutMarginsFollowReadableWidth = false
}

So this is comparable to the handling of the preserving of superview margins, which is done by code with preservesSuperviewLayoutMargins = true/false.

From the api docs for cellLayoutMarginsFollowReadableWidth:


A Boolean value that indicates whether the cell margins are derived
from the width of the readable content guide.

This property is the equivalent to the option in Interface Builder.

2) UIViews

Sadly there is no single property like the above publicly available with the UIKit api. But the quote from the api docs already gives a hint:

Instead of a single property to toggle with true/false, every UIView provides a readableContentGuide property. That property is part of the new UILayoutGuide class (iOS 9 and later).

To understand what that is and how to use it, let’s take a look at what the “Follow Readable Width” option does: If you enable that option in interface builder, a set of constraints is automatically added to that view.

The readableContentGuidebasically provides you with a set of these anchor points.

If you want to pin your layout to the readable width by code, create a group of NSLayoutConstraints and connect them to the anchor points of readableContentGuide. This has to be wrapped in an #available(iOS 9, *) block, too.

Updated 04/21/16
Added info about UITableView cellLayoutMarginsFollowReadableWidth

Updated 04/22/16
Rephrased to make things clear. Thanks to @matt for making me aware of!

Hope this helps!