Parus 是一个用于 AutoLayout 的代码中的小 Objective-C DSL。
NSLayoutConstraint
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeLeft
multiplier:2.0
constant:10];
使用 Parus
PVLeftOf(view).equalTo.leftOf(superview).multipliedTo(2).plus(10).asConstraint;
使用默认值可以使其更短
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:10];
PVLeftOf(view).equalTo.constant(10).asConstraint;
简单的 VFL 约束
[NSLayoutConstraint constraintsWithVisualFormat:@"|-padding-[view]-padding-|"
options:(NSLayoutFormatAlignAllTop |
NSLayoutFormatDirectionLeadingToTrailing)
metrics:@{@"padding" : @20}
views:NSDictionaryOfVariableBindings(view)];
PVVFL(@"|-padding-[view]-padding-|").alignAllTop.fromLeadingToTrailing.withViews(NSDictionaryOfVariableBindings(view)).metrics(@{@"padding": @20}).asArray;
带有特殊的面具和默认值
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view1][view2][view3]|"
options:(NSLayoutFormatAlignAllLeft |
NSLayoutFormatAlignAllRight)
metrics:nil
views:NSDictionaryOfVariableBindings(view1, view2, view3)];
PVVFL(@"V:|[view1][view2][view3]|").alignAllLeftAndRight.withViews(NSDictionaryOfVariableBindings(view1, view2, view3)).asArray;
有一个可用的功能可以帮助您分组约束并生成更少的代码。享受吧!
[someView addConstraint:[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:10]];
[someView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view1][view2][view3]|"
options:(NSLayoutFormatAlignAllLeft |
NSLayoutFormatAlignAllRight)
metrics:nil
views:NSDictionaryOfVariableBindings(view1, view2, view3)]];
[someView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view1]|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(view1)]];
[someView addConstraints:PVGroup(@[PVLeftOf(view).equalTo.constant(10),
PVVFL(@"V:|[view1][view2][view3]|").alignAllLeftAndRight,
PVVFL(@"H:|[view1]|")
]).withViews(NSDictionaryOfVariableBindings(view1, view2, view3)).asArray];
您还可以使用常规的 NSLayoutConstraint
或 NSLayoutConstraint
的 NSArray
作为 PVGroup
的项目。以下代码是完全可接受的
NSLayoutConstraint* usualConstraint = [NSLayoutConstraint constraintWithItem:... blablabla very long constraint definition ...];
NSArray* usualConstraints = [NSLayoutConstraint constraintsWithVisualFormat:... blabla ...];
[someView addConstraints:PVGroup(@[usualConstraint, usualConstraints]).asArray];
使用 cocoapods!
pod 'Parus'
#import <Parus/Parus.h>
访问我们的 wiki