iOS 9 - Xcode:以编程方式“遵循可读宽度”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34864677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:26:39  来源:igfitidea点击:

iOS 9 - Xcode: "Follows Readable Width" programmatically

iosxcodeswiftuiviewios9

提问by Mario A Guzman

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.

在 iOS 9 中,Xcode SIZE 检查器中有一个名为“Follows Readable Width”的选项,它将在视图的左侧/右侧放置填充,这样它就不会延伸到很远(尤其是在 iPad 上)以便使内容更易于阅读。

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

(在 iPhone 6/6s plus 上横向放置时,您可以在 Twitter 应用程序上看到此示例。)

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.

无论如何,我似乎无法找到如何以编程方式执行此操作。在 Xcode SIZE 检查器中,它只是您设置的一个复选框。这显然是一个布尔值,您可以在任何 UIView 或从它继承的任何内容上设置。我似乎无法找到任何接近它的东西。

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

有人曾经以编程方式设置过吗?请参阅随附的屏幕截图。

Oh, I canfind the 'Preserve Superview Margins' just fine, just not the 'Follows Readable Width' property.

哦,我可以找到“保留超级视图边距”很好,而不是“遵循可读宽度”属性。

enter image description here

在此处输入图片说明

回答by Frederik A. Winkelsdorf

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

您可以在 Interface Builder 中设置的“Follow Readable Width”选项由代码以两种不同的方式处理:

1) UITableViews

1) UITableViews

There is a property for UITableViewto trigger the readable width:

有一个用于UITableView触发可读宽度的属性:

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.

所以这类似于处理保留 superview 边距,这是由代码完成的preservesSuperviewLayoutMargins = true/false

From the api docs for cellLayoutMarginsFollowReadableWidth:

来自 api 文档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.

此属性等效于 Interface Builder 中的选项。

2) UIViews

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:

遗憾的是,UIKit api 没有像上面那样公开可用的单一属性。但是 api 文档中的引用已经给出了一个提示:

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

true/false每个 UIView 都提供一个readableContentGuide属性,而不是要切换的单个属性。该属性是新UILayoutGuide类(iOS 9 及更高版本)的一部分。

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.

要了解它是什么以及如何使用它,让我们看看“Follow Readable Width”选项的作用:如果您在界面构建器中启用该选项,一组约束将自动添加到该视图中。

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

readableContentGuide基本上提供了一组这些锚点。

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.

如果你想通过代码将你的布局固定到可读的宽度,创建一组 NSLayoutConstraints 并将它们连接到readableContentGuide. 这也必须包装在一个#available(iOS 9, *)块中。

Updated 04/21/16Added info about UITableView cellLayoutMarginsFollowReadableWidth

更新 04/21/16添加了有关 UITableView cellLayoutMarginsFollowReadableWidth 的信息

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

2016 年 4 月 22 日更新,重新措辞使事情清楚。感谢@matt 让我知道!