Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使两个自动布局约束在代码中相等?_Ios_Objective C_Uitableview_Autolayout_Nslayoutconstraint - Fatal编程技术网

Ios 使两个自动布局约束在代码中相等?

Ios 使两个自动布局约束在代码中相等?,ios,objective-c,uitableview,autolayout,nslayoutconstraint,Ios,Objective C,Uitableview,Autolayout,Nslayoutconstraint,我第一次尝试自动布局(而不是使用IB/故事板),我尝试在UITableViewCell中对标题标签和细节标签进行简单的垂直对齐,布局如下: —————— Padding Title Label Spacing (3px) Detail Label Padding —————— // Constrain the title label at the top of the label container. [title makeConstraints:^(MASConstraintMaker *

我第一次尝试自动布局(而不是使用IB/故事板),我尝试在
UITableViewCell
中对标题标签和细节标签进行简单的垂直对齐,布局如下:

—————— 
Padding
Title Label
Spacing (3px)
Detail Label
Padding
——————
// Constrain the title label at the top of the label container.
[title makeConstraints:^(MASConstraintMaker *make) {
    make.top.greaterThanOrEqualTo(self.contentView.top).with.priorityLow();
}];

// Constrain the detail label to the bottom of the label container.
[detail makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.titleLabel.bottom).with.offset(3).with.priorityHigh();
    make.bottom.lessThanOrEqualTo(self.contentView.bottom).with.priorityLow();
}];
我要做的是让两个“填充”约束始终保持相等。其他一切都可以工作,但标签要么在单元格的顶部,要么在单元格的底部

我实际上是用砖石来实现的,它看起来是这样的:

—————— 
Padding
Title Label
Spacing (3px)
Detail Label
Padding
——————
// Constrain the title label at the top of the label container.
[title makeConstraints:^(MASConstraintMaker *make) {
    make.top.greaterThanOrEqualTo(self.contentView.top).with.priorityLow();
}];

// Constrain the detail label to the bottom of the label container.
[detail makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.titleLabel.bottom).with.offset(3).with.priorityHigh();
    make.bottom.lessThanOrEqualTo(self.contentView.bottom).with.priorityLow();
}];
我需要从<代码>标题> />代码>约束和<代码> >从代码>详细> <代码>约束始终保持相同的值,使标题/间距/细节位于中间。


我在这里遗漏了什么?

这是仅通过常规约束无法做到的

不能使空间彼此相等

您需要做的是使用不可见的“间隔”视图并使其高度相等

所以你会有一些像

// in pseudo VFL
V:|[topSpacer][titleLabel]-[subTitleLabel][bottomSpacer(==topSpacer)]|
使上间隔器和下间隔器
alpha=0
hidden=YES
将隐藏它们,但它们将充当布局脚手架


通过这样做,标题标签上方的间隙将与字幕标签下方的间隙相同。

这是仅通过常规约束无法实现的

不能使空间彼此相等

您需要做的是使用不可见的“间隔”视图并使其高度相等

所以你会有一些像

// in pseudo VFL
V:|[topSpacer][titleLabel]-[subTitleLabel][bottomSpacer(==topSpacer)]|
使上间隔器和下间隔器
alpha=0
hidden=YES
将隐藏它们,但它们将充当布局脚手架


这样,标题标签上方的间隙将与副标题标签下方的间隙相同。

将标题和详细信息标签放入子视图中。然后针对封闭单元为该子视图创建约束。由于希望顶部和底部填充相等,因此可以简单地选择垂直居中作为约束


另外,假设iOS 8,我相信您可以允许表格单元格自身调整大小以适应子视图。

将标题和详细信息标签放入子视图中。然后针对封闭单元为该子视图创建约束。由于希望顶部和底部填充相等,因此可以简单地选择垂直居中作为约束


另外,假设iOS 8,我相信您可以允许表格单元格自身调整大小以适应子视图。

哇,这真的是最简单的方法吗?对于一些你认为非常简单的东西,这是一个很大的骗局。是的,不幸的是,AutoLayout中的
空格
。i、 e.意见之间的差距。不是对象本身,因此不能等同于数字以外的任何东西。当然,您可以通过编程方式计算间隙,并在计算完约束后更新约束,但这与使用自动布局的目的背道而驰。最简单/唯一的方法是使用不可见视图作为“脚手架”。不过这是一种常见的技术。很多人使用它。是的,最初的技术就是我在自动布局阵痛开始之前所做的。我来试试脚手架,看看它有多显眼。非常感谢,先生:)哇,这真的是最简单的方法吗?对于一些你认为非常简单的东西,这是一个很大的骗局。是的,不幸的是,AutoLayout中的
空格
。i、 e.意见之间的差距。不是对象本身,因此不能等同于数字以外的任何东西。当然,您可以通过编程方式计算间隙,并在计算完约束后更新约束,但这与使用自动布局的目的背道而驰。最简单/唯一的方法是使用不可见视图作为“脚手架”。不过这是一种常见的技术。很多人使用它。是的,最初的技术就是我在自动布局阵痛开始之前所做的。我来试试脚手架,看看它有多显眼。非常感谢,先生:)