在代码中使两个AutoLayout约束相等?

Luk*_*uke 1 objective-c uitableview ios autolayout nslayoutconstraint

我第一次穿过AutoLayout(并没有使用IB /故事板这样做),我试图在UITableViewCell标题标签和细节标签中做一些简单的垂直对齐,像这个:

—————— 
Padding
Title Label
Spacing (3px)
Detail Label
Padding
——————
Run Code Online (Sandbox Code Playgroud)

我正在努力的部分是让两个"填充"约束在任何时候保持相等.其他一切都有效,但标签位于单元格的顶部或底部.

我实际上正在使用Masonry来实现它,它看起来像这样:

// 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();
}];
Run Code Online (Sandbox Code Playgroud)

我需要make.top来自title约束的make.bottom线和来自约束的线detail始终保持相同的值,以便标题/间距/细节位于中间.

我在这里错过了什么?

Fog*_*ter 6

这是你不能通过正常约束做到的事情.

你不能让空格彼此相等.

你需要做的是使用不可见的"间隔"视图,并使它们的高度相等.

所以你会有类似......

// in pseudo VFL
V:|[topSpacer][titleLabel]-[subTitleLabel][bottomSpacer(==topSpacer)]|
Run Code Online (Sandbox Code Playgroud)

制作topSpacer和bottomSpacer alpha = 0hidden = YES它将隐藏它们,但它们将充当布局脚手架.

通过这样做,titleLabel上方的间隙将与subTitleLabel下方的间隙相同.