简体   繁体   English

自动布局-复杂的约束

[英]Auto layout - complex constraints

I'm trying to display content in a table cell using auto layout, programmatically. 我正在尝试使用自动布局以编程方式在表格单元格中显示内容。 I'd like for the content to display as follows: 我希望内容显示如下:

[title] [标题]
[image] [date] [图片] [日期]
[long string of text, spanning the width of the table, maximum of two lines] [长字符串,跨越表格的宽度,最多两行]

My code looks like this: 我的代码如下所示:

-(NSArray *)constraints {
    NSMutableArray * constraints = [[NSMutableArray alloc]init];
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_titleLabel, _descriptionLabel, _dateLabel, _ratingBubbleView);

    NSDictionary *metrics = @{@"padding":@(kPadding)};

    NSString *const kVertical = @"V:|-(>=0,<=padding)-[_titleLabel]-(<=padding)-[_ratingBubbleView]-(<=padding)-[_descriptionLabel]-(>=0,<=padding)-|";
    NSString *const kVertical2 = @"V:|-(>=0,<=padding)-[_titleLabel]-(<=padding)-[_dateLabel]-(<=padding)-[_descriptionLabel]-(>=0,<=padding)-|";
    NSString *const kHorizontalDescriptionLabel = @"H:|-padding-[_descriptionLabel]-padding-|";
    NSString *const kHorizontalTitleLabel = @"H:|-padding-[_titleLabel]";
    NSString *const kHorizontalDateLabel = @"H:|-padding-[_ratingBubbleView]-padding-[_dateLabel]";

    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kVertical options:0 metrics:metrics views:viewsDictionary]];
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kVertical2 options:0 metrics:metrics views:viewsDictionary]];
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kHorizontalDescriptionLabel options:0 metrics:metrics views:viewsDictionary]];
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kHorizontalTitleLabel options:0 metrics:metrics views:viewsDictionary]];
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kHorizontalDateLabel options:0 metrics:metrics views:viewsDictionary]];

    return constraints;
}

This is the result: 结果如下: 这是结果

OK, I'm not going to try a fix your code. 好的,我不会尝试修复您的代码。 I'm just going to create constraints that I would use to achieve your layout. 我将创建一些约束,以用于实现您的布局。 I'll put the thought process in comments. 我将思考过程放在注释中。

First get a nice vertical layout going... 首先得到一个不错的垂直布局...

// I'm just using standard padding to make it easier to read.
// Also, I'd avoid the variable padding stuff. Just set it to a fixed value.
// i.e. ==padding not (>=0, <=padding). That's confusing to read and ambiguous.
@"V:|-[titleLabel]-[ratingBubbleView]-[descriptionLabel]-|"

Then go through layer by layer adding horizontal constraints... 然后逐层添加水平约束...

// constraint the trailing edge too. You never know if you'll get a stupidly
// long title. You want to stop it colliding with the end of the screen.
// use >= here. The label will try to take it's intrinsic content size
// i.e. the smallest size to fit the text. Until it can't and then it will
// break it's content size to keep your >= constraint.
@"|-[titleLabel]->=20-|"

// when adding this you need the option "NSLayoutFormatAlignAllBottom".
@"|-[ratingBubbleView]-[dateLabel]->=20-|"

@"|-[descriptionLabel]-|"

Try not to "over constrain" your view. 尽量不要“过度约束”您的视图。 In your code you are constraining the same views with multiple constraints (like descriptionLabel to the bottom of the superview). 在您的代码中,您要使用多个约束来约束相同的视图(例如,superview底部的descriptionLabel)。

Once they're defined they don't need to be defined again. 定义它们后,就无需再次定义它们。

Again, with the padding. 再次,与填充。 Just use padding rather than >=padding . 只需使用padding而不是>=padding Does >=20 mean 20, 21.5, or 320? > = 20表示20、21.5或320? The inequality is ambiguous when laying out. 布局时,不平等是不明确的。

Also, In my constraints I have used the layout option to constrain the vertical axis of the date label to the rating view. 另外,在我的约束中,我使用了layout选项将日期标签的垂直轴约束到评级视图。 ie "Stay in line vertically with the rating view". 即“与评级视图垂直保持一致”。 Instead of constraining against the title label and stuff... This means I only need to define the position of that line of UI once. 而不是限制标题标签和内容……这意味着我只需要定义一次该UI行的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM