ios 約束(七)

用宏來創建約束規則

    用約束規則來排布控件的位置是相當可靠的。不過,就其本身來說,他們非常繁瑣而且特別冗長。開發者要一次又一次的編寫很難懂的方法調用語句。

    約束規則調用起來也特別麻煩。一個簡單的拼寫錯誤就會耗費很多時間,而且許多應用程序所使用的約束規則都是一樣的。如果可以預先定義一些宏,那麼就能把排布視圖所用的代碼寫的更易懂且更加可靠。假如要把某視圖與另外一個視圖居中對齊,那麼直接使用名爲CENTER_VIEW的宏就好,而不用每次都編寫約束規則並調試他們。

    這些宏以一種相當簡單的方式來產生約束規則。請注意,他們並不把創建出來的NSLayoutConstraint對象返回給調用者,而是將這些約束規則添加到適當的視圖裏。如果需要獲取這些新創建好的約束規則,以便稍後移除,那麼可以自己添加一些能返回相關約束規則的宏。

#define PREPCONSTRAINTS(VIEW) [VIEW setTranslatesAutoresizingMaskIntoConstraints:NO]
#define CONSTRAIN(PARENT,VIEW,FORMAT) [PARENT addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:(FORMAT) options:0 metrics:nil views:NSDictionaryOfVariableBindings(VIEW)]]
#define CONSTRAIN_VIEWS(PARENT,FORMAT,BINDINGS) [PARENT addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:(FORMAT) options:0 metrics:nil views:BINDINGS]]

#define STRETCH_VIEW_H(PARENT,VIEW) CONSTRAIN(PARENT,VIEW,@"H:|["#VIEW"(>=0)]|")

#define STRETCH_VIEW_V(PARENT,VIEW) CONSTRAIN(PARENT,VIEW,@"V:|["#VIEW"(>=0)]|")
#define STRETCH_VIEW(PARENT,VIEW) {STRETCH_VIEW_H(PARENT,VIEW);STRETCH_VIEW_V(PARENT,VIEW)}

#define CENTER_VIEW_H(PARENT,VIEW) [PARENT addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:PARENT attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]]
#define CENTER_VIEW_V(PARENT,VIEW) [PARENT addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:PARENT attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]]
#define CENTER_VIEW(PARENT,VIEW) {ENTER_VIEW_H(PARENT,VIEW);ENTER_VIEW_V(PARENT,VIEW)}

#define ALIGN_VIEW_LEFT(PARENT,VIEW) [PARENT addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:PARENT attribute:NSLayoutAttributeLeft multiplier:1 constant:0]]

#define ALIGN_VIEW_RIGHT(PARENT,VIEW) [PARENT addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:PARENT attribute:NSLayoutAttributeRight multiplier:1 constant:0]]

#define ALIGN_VIEW_TOP(PARENT,VIEW) [PARENT addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:PARENT attribute:NSLayoutAttributeTop multiplier:1 constant:0]]

#define ALIGN_VIEW_BOTTOM(PARENT,VIEW) [PARENT addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:PARENT attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]

#define CONSTRAIN_WIDTH(VIEW,WIDTH) [VIEW addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:WIDTH]]

#define CONSTRAIN_HEIGHT(VIEW,HEIGHT) [VIEW addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:HEIGHT]]

#define CONSTRAIN_SIZE(VIEW,HEIGHT,WIDTH) {CONSTRAIN_WIDTH(VIEW,WIDTH);CONSTRAIN_HEIGHT(VIEW,HEIGHT)}

#define CONTRAIN_ASPECT(VIEW,ASPECT) [VIEW addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:VIEW attribute:NSLayoutAttributeHeight multiplier:(ASPECT) constant:0]]

#define CONTRAIN_ORDER_H(PARENT,VIEW1,VIEW2) [PARENT addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:(@"H:["#VIEW1"]->=0-["#VIEW2"]") options:0 metrics:nil views:NSDictionaryOfVariableBindings(VIEW1,VIEW2)]]

#define CONTRAIN_ORDER_V(PARENT,VIEW1,VIEW2) [PARENT addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:(@"V:["#VIEW1"]->=0-["#VIEW2"]") options:0 metrics:nil views:NSDictionaryOfVariableBindings(VIEW1,VIEW2)]]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章