23

根据Cocoa Auto Layout Guide,我可以使用视觉约束格式语言中的破折号来“表示标准 Aqua 空间:”

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[a]-[b]"
                                                             options:0
                                                             metrics:nil
                                                               views:viewDict]];

但是,如果我在不使用可视格式语言的情况下构建约束,我似乎找不到NSLayout...允许我做同样事情的常量或方法:

[self addConstraint:[NSLayoutConstraint constraintWithItem:a
                                                 attribute:NSLayoutAttributeTrailing
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:b
                                                 attribute:NSLayoutAttributeLeading
                                                multiplier:1.0f
                                                  constant:<# ??? #>]];

在这种情况下,是否有一个常量(或其他值或方法)可以用来定义 Aqua 空间?

4

3 回答 3

32

我发现“标准 Aqua 空间”在兄弟视图之间为 8.0,在视图与其父视图之间为 20.0。

NSView* view = [NSView new] ;
NSLayoutConstraint* constraintWithStandardConstantBetweenSiblings = [NSLayoutConstraint constraintsWithVisualFormat:@"[view]-[view]"  options:0  metrics:nil  views:NSDictionaryOfVariableBindings(view) ] [0] ;
CGFloat standardConstantBetweenSiblings = constraintWithStandardConstantBetweenSiblings.constant ;    // 8.0

NSView* superview = [NSView new] ;
[superview addSubview:view] ;
NSLayoutConstraint* constraintWithStandardConstantBetweenSuperview = [NSLayoutConstraint constraintsWithVisualFormat:@"[view]-|"  options:0  metrics:nil  views:NSDictionaryOfVariableBindings(view) ] [0] ;
CGFloat standardConstantBetweenSuperview = constraintWithStandardConstantBetweenSuperview.constant ;    // 20.0
于 2012-12-27T16:03:05.610 回答
10

对于 iOS 用户:

#import "NSLayoutConstraint+StandardOffsets.h"

@implementation NSLayoutConstraint (StandardOffsets)

+ (CGFloat)standardConstantBetweenSiblings
{
    static CGFloat value;

    if(!isnormal(value)) {
        UIView *view = [UIView new] ;
        NSLayoutConstraint* constraintWithStandardConstantBetweenSiblings = [NSLayoutConstraint constraintsWithVisualFormat:@"[view]-[view]"  options:0  metrics:nil  views:NSDictionaryOfVariableBindings(view) ] [0] ;
        value = constraintWithStandardConstantBetweenSiblings.constant ;    // 8.0
    }
    return value;
}

+ (CGFloat)standardConstantBetweenSuperview
{
    static CGFloat value;

    if(!isnormal(value)) {
        UIView *view = [UIView new] ;
        UIView *superview = [UIView new] ;
        [superview addSubview:view] ;
        NSLayoutConstraint* constraintWithStandardConstantBetweenSuperview = [NSLayoutConstraint constraintsWithVisualFormat:@"[view]-|"  options:0  metrics:nil  views:NSDictionaryOfVariableBindings(view) ] [0] ;
        value = constraintWithStandardConstantBetweenSuperview.constant ;    // 20.0
    }
    return value;
}

@end

PS:我输入了一个错误报告,头文件中没有提供常量!

于 2013-09-28T01:55:01.590 回答
5

根据John Sauer 的回答,我最终在 NSLayoutConstraint 类别上编写了几个方法来查找常量:

  • +standardAquaSpaceConstraintFromItem:toItem:返回使用可视格式语言构造的单个 NSLayoutConstraint;它断言从格式生成的约束数组只有一个项目,然后获取该项目并将其返回。
  • +standardAquaSpaceFromItem:toItem:从前一个方法中将常量从约束中拉出,并将其作为 CGFloat 返回。

这样,如果我需要进行计算,我可以自己获取常量值,或者只获取具有正确间距的单个布局约束(例如,分配给约束@property或直接添加到我的视图中)。

于 2012-12-27T23:11:18.230 回答