逐條建立 NSLayoutConstraint 冗長又容易出錯。Apple 因此提供了 VFL(Visual Format Language),用一段字串描述視圖之間的相對關係,再一次產生整組約束。
+ (NSArray *)constraintsWithVisualFormat:(NSString *)format
options:(NSLayoutFormatOptions)opts
metrics:(NSDictionary *)metrics
views:(NSDictionary *)views;format:VFL 字串,例如 @"H:|-10-[view]-20-|"。opts:列舉參數,預設為 0。metrics:自訂字典,字典裡的 key 可以寫進 format 字串,編譯時會自動替換成對應的 value。views:約束中要用到的 view 對照表。NSLayoutFormatOptions 的完整定義如下:
typedef NS_OPTIONS(NSUInteger, NSLayoutFormatOptions) {
NSLayoutFormatAlignAllLeft = (1 << NSLayoutAttributeLeft),
NSLayoutFormatAlignAllRight = (1 << NSLayoutAttributeRight),
NSLayoutFormatAlignAllTop = (1 << NSLayoutAttributeTop),
NSLayoutFormatAlignAllBottom = (1 << NSLayoutAttributeBottom),
NSLayoutFormatAlignAllLeading = (1 << NSLayoutAttributeLeading),
NSLayoutFormatAlignAllTrailing = (1 << NSLayoutAttributeTrailing),
NSLayoutFormatAlignAllCenterX = (1 << NSLayoutAttributeCenterX),
NSLayoutFormatAlignAllCenterY = (1 << NSLayoutAttributeCenterY),
NSLayoutFormatAlignAllBaseline = (1 << NSLayoutAttributeBaseline),
NSLayoutFormatAlignAllLastBaseline = NSLayoutFormatAlignAllBaseline,
NSLayoutFormatAlignAllFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0)
= (1 << NSLayoutAttributeFirstBaseline),
NSLayoutFormatAlignmentMask = 0xFFFF,
/* choose only one of these three */
NSLayoutFormatDirectionLeadingToTrailing = 0 << 16, // default
NSLayoutFormatDirectionLeftToRight = 1 << 16,
NSLayoutFormatDirectionRightToLeft = 2 << 16,
NSLayoutFormatDirectionMask = 0x3 << 16,
};metrics 讓你把魔法數字從字串裡抽出來,改一次就能全域生效:
NSDictionary *metrics = @{@"left": @5, @"right": @5, @"height": @150.0};
NSString *format = @"|-left-[view]-right-|";不需要時傳 nil 即可。
views 則是把區域變數對應成字串裡使用的名稱。實務上最常搭配 NSDictionaryOfVariableBindings 這個宏:
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(view);
它等價於 [NSDictionary dictionaryWithObjectsAndKeys:view, @"view", nil],省下手寫 key 的麻煩,也不容易打錯字。
例如 @"H:|-10-[view]-20-|" 的意思是:view 距離父視圖左邊 10 點、右邊 20 點——兩個端點都固定了,寬度自然也就被確定下來。
V: 表示縱向;H: 表示橫向(未宣告方向時預設為 H:)。| 表示父視圖邊緣。- 表示距離。[view] 表示子視圖。>= 表示視圖間距、寬度或高度必須大於或等於某個值。<= 表示必須小於或等於某個值。== 表示必須等於某個值。@ 用來標示 >=、<=、== 的優先級,數值越大越優先,最大為 1000。
|-[view]-|:視圖處在父視圖的左右邊緣之內。|-[view]:視圖貼齊父視圖的左邊緣。[view]:視圖與父視圖左邊對齊。-[view]-:只用來設定視圖的寬度或高度。|-30.0-[view]-30.0-|:離父視圖左右間距皆為 30。[view(200.0)]:視圖寬度為 200。|-[view(view1)]-[view1]-|:兩個視圖寬度相同,並且在父視圖左右邊緣之內。V:|-[view(50.0)]:視圖高度為 50。V:|-(==padding)-[imageView]->=0-[button]-(==padding)-|:與父視圖上下距離為 padding,兩個視圖間距必須大於或等於 0,並與底部保持 padding。[wideView(>=60@700)]:視圖寬度至少 60,優先級為 700。
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor brownColor];
view.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:view];
// 透過宏映射成 [NSDictionary dictionaryWithObjectsAndKeys:view, @"view", nil]
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(view);
// 約束 1:橫向
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[view]-20-|"
options:0
metrics:nil
views:viewsDictionary]];
// 約束 2:縱向
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[view]-200-|"
options:0
metrics:nil
views:viewsDictionary]];務必記得把 translatesAutoresizingMaskIntoConstraints 設為 NO,否則系統仍會依 autoresizing mask 產生額外約束,造成衝突警告。