我試圖保持儘可能接近蘋果的實現,承認的NSLayoutConstraint
存在,所以我只是定義了一組Concise Auto Layout macros,從屬性和關係刪除前綴和包裹約束創建在構造函數函數宏(也省略,我從來沒有使用乘法器參數):
Constraint
ConstantConstraint
VisualConstraints
VisualConstraintWithMetrics
要附加多個簡單的限制,以查看,我將它們包裝在數組文本。我總是在一個符號前面加上非零常數,以強調位移。在實踐中它看起來像這樣(實例變量reffer到的意見):
[self.view addConstraints:
@[Constraint(_verticalSeparator, CenterX, Equal, _top, CenterX, 0),
Constraint(_verticalSeparator, CenterY, Equal, _top, CenterY, +22),
Constraint(_verticalSeparator, Height, Equal, _localWeather, Height, 0),
Constraint(_localWeather, CenterY, Equal, _verticalSeparator, CenterY, 0),
Constraint(_addLocation, CenterY, Equal, _verticalSeparator, CenterY, 0),
Constraint(_touchDown, Trailing, Equal, _verticalSeparator, Trailing, -1),
Constraint(_touchDown, CenterY, Equal, _localWeather, CenterY, 0),
Constraint(_touchDown, Width, Equal, _localWeather, Width, +26),
Constraint(_touchDown, Height, Equal, _localWeather, Height, 0)
]];
id f = @"[_localWeather]-space-[_verticalSeparator]-space-[_addLocation]";
[self.view addConstraints:
VisualConstraintWithMetrics(f, @{@"space": @11},
_localWeather, _verticalSeparator, _addLocation)];
[_tableCell addConstraint:ConstantConstraint(_tableCell, Height, Equal, 44)];
當意見組處理所有共享相同的設置,我列舉了數組文字是這樣的:
[@[_top, _middle, _bottom, _touchDown, _verticalSeparator]
enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
view.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:view];
}];
灌裝在superview
水平經常發生的是,它被提升到了自己的宏:
[@[_top, _middle, _bottom] enumerateObjectsUsingBlock:horizontallyFillSuperview];
爲我的風格演變我會更新這個答案...
+1用於命名您的來源。 – Simon