Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
834 views
in Technique[技术] by (71.8m points)

macos - What constant can I use for the default Aqua space in Autolayout?

According to the Cocoa Auto Layout Guide, I can use a dash in the visual constraint format language to "denote the standard Aqua space:"

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

However, I can't seem to find an NSLayout... constant or method that allows me to do the same thing if I'm building a constraint without using the visual format language:

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

Is there a constant (or another value or method) that I can use to define the Aqua space in such a situation?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've found the "standard Aqua space" to be 8.0 between sibling views, and 20.0 between a view and its superview.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...