制作更易读(和对)更易写入的 Auto Layout 宏(取消使用字符串)。
不使用(视觉格式)字符串意味着您可以直接使用变量和值。适用于 iOS 和 OS X。
[view addConstraints:@[
ALIGN_LEFT(view1, view2),
ALIGN_TOP(view1, view2),
PIN_WIDTH(view1, view2, TIMES(0.5))
]];
AutoLayoutMacros | 普通 Auto Layout |
---|---|
ALIGN_TOP(view1, view2) |
[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0] |
INSET_LEFT(view1, 50) |
[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view1.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:50.0] |
PIN_WIDTH(view1, LE(100)) |
[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100.0] |
PIN_HEIGHT(view1, view2, TIMES(2.0)) |
[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:2.0 constant:0.0] |
HSPACE(view1, view2, 50, PRIO(500)) |
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0] constraint.priority = 500; |
ALIGN_LEADING(view1, view2)
ALIGN_TRAILING(view1, view2)
ALIGN_LEFT(view1, view2)
ALIGN_RIGHT(view1, view2)
ALIGN_CENTER_X(view1, view2)
ALIGN_TOP(view1, view2)
ALIGN_BOTTOM(view1, view2)
ALIGN_CENTER_Y(view1, view2)
ALIGN_BASELINE(view1, view2)
ALIGN_LEADING(view1, view2, 20)
ALIGN_LEADING(view1, GE(view2))
ALIGN_LEADING(view1, LE(view2, 10))
INSET_LEADING(view1)
INSET_TRAILING(view1)
INSET_LEFT(view1)
INSET_RIGHT(view1)
INSET_CENTER_X(view1)
INSET_TOP(view1)
INSET_BOTTOM(view1)
INSET_CENTER_Y(view1)
INSET_BASELINE(view1)
INSET_LEADING(view1, -20)
INSET_LEADING(view1, GE(50))
PIN_WIDTH(view1, view2)
PIN_HEIGHT(view1, view2)
PIN_WIDTH(view1, view2, 20)
PIN_WIDTH(view1, view2, TIMES(2))
PIN_WIDTH(view1, view2, TIMES(2), 10)
PIN_WIDTH(view1, EQ(view2, TIMES(2), 10))
PIN_WIDTH(view1, LE(view2, 50), PRIO(500))
PIN_WIDTH(view1, 100)
ALIGN_HEIGHT(view1, 100)
PIN_WIDTH(view1, GE(100))
PIN_WIDTH(view1, 100, LOW)
HSPACE(view1, view2)
VSPACE(view1, view2)
HSPACE(view1, view2, 10)
HSPACE(view1, view2, GE(10))
HSPACE(view1, view2, LE(10), HIGH)
HSPACE(view1, view2, 10, REQUIRED)
默认情况下,所有宏都使用 ALM_
前缀,例如,如果您导入 AutoLayoutMacros.h
,则为 ALM_ALIGN_LEFT
。上面的较短的宏在 AutoLayoutMacrosShort.h
中可用。
请不要使用以 _ALM 开始的函数。这些是内部使用的。
如果您不使用 CocoaPods,请将仓库中的 AutoLayoutMacros 目录复制到您的项目中。