【问题标题】:Understanding visual format constraints in iOS Objective C了解 iOS Objective C 中的视觉格式约束
【发布时间】:2017-11-20 11:19:18
【问题描述】:

我知道在 InterfaceBuilder ex 中设置约束。前导、尾随、顶部、底部、固定宽度等。 我找到了一些约束代码,我不知道这段代码试图设置哪个约束,下面的视觉格式约束到底是什么意思?

  NSDictionary *binding = @{@"v" : self.view};
    NSDictionary *metrics = @{@"height" : @(self.height)};
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]|" options:0 metrics:nil views:binding]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[v(==height)]|" options:0 metrics:metrics views:binding]];

【问题讨论】:

  • 在该文档cmd +f“v”上。没结果。你能告诉我什么是“v”和“height”吗?
  • 以下网站将为您提供帮助。 autolayoutconstraints.com
  • 不要再坚持使用 VFL VFL 不能考虑安全区域布局指南,它只能考虑顶级布局指南。你可以编写完美的 VFL 来搞砸你在 iPhoneX 中的 UI
  • 有很多令人信服的论点来避免在 iOS 开发的这个阶段使用 VFL。正如@SandeepBhandari 提到的那样,不能使用安全区域布局指南。也极容易出现开发人员错误。如果您想了解现有的约束,我希望将它们转换为基于锚的。如果你想创建新的约束,我建议改为基于锚。

标签: ios objective-c nslayoutconstraint


【解决方案1】:

H:|[v]|

H 表示约束是水平添加的,类似地V 是垂直添加的。

| 表示绑定字典所指示的超级视图。 NSDictionary *binding

[v] 代表视图本身。

所以H:|[v]| 解析为具有0 常量的前导和尾随约束。

V:[v(==高度)]|

与此类似,视图被赋予一个底部约束和一个高度约束,并带有常量height,如NSDictionary *metrics 中所述。

更多信息请参考https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage.html

【讨论】:

  • 对于H,左|是指前导,同样对于V,左|是指上,右|是指下。
  • 明白了,很好的解释!
  • 你也可以answer this吗?
【解决方案2】:

正如 GoodSp33d 向我建议的那样。

你的约束是-

(1) leading & trailingself.view0 我已将上述约束转换为不同的方式

(2) ContentView 的底部 分配给 self.view

(3)固定高度限制

以另一种形式约束-

    [self.contentView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
    [self.contentView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES;
    [self.contentView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;


    NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.view
                                                                        attribute:NSLayoutAttributeHeight
                                                                        relatedBy:NSLayoutRelationEqual
                                                                           toItem:nil
                                                                        attribute:NSLayoutAttributeNotAnAttribute
                                                                       multiplier:1.0
                                                                         constant:self.height];
    [self.view addConstraint:heightConstraint];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多