2013-12-18 76 views
2

我想學習可視化格式語言,在這種情況下,我想在視圖的頂部相鄰的兩個按鈕。iOS的VFL顯示兩個按鈕nextother

這是我的代碼,但沒有顯示?

UIButton *button1 = [[UIButton alloc] init]; 
[button1 setBackgroundColor:[UIColor blueColor]]; 
UIButton *button2 = [[UIButton alloc] init]; 
[button2 setBackgroundColor:[UIColor redColor]]; 
NSDictionary *views = NSDictionaryOfVariableBindings(button1, button2); 
NSLog(@"%@", [views allKeys]); 
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"[button1(==button2)]" options:0 metrics:Nil views:views]; 
[self.view addSubview:button1]; 
[self.view addSubview:button2]; 
[self.view addConstraints:constraints]; 

多虧了答案,這是我現在的代碼:

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem]; 
button1.translatesAutoresizingMaskIntoConstraints = NO; 
[button1 setTitle:@"Button" forState:UIControlStateNormal]; 
[button1 setBackgroundColor:[UIColor redColor]]; 
[self.view addSubview:button1]; 

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem]; 
button2.translatesAutoresizingMaskIntoConstraints = NO; 
[button2 setTitle:@"Button" forState:UIControlStateNormal]; 
[button2 setBackgroundColor:[UIColor blueColor]]; 
[self.view addSubview:button2]; 

NSDictionary *views = NSDictionaryOfVariableBindings(button1, button2); 
NSString *const kHConstraint = @"|-[button1(==button2)]-[button2]-|"; 
NSString *const kVConstraint = @"V:|-[button1(==44)]"; 
NSString *const kVConstraint2 = @"V:|-[button2(==44)]"; 
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:kHConstraint options:0 metrics:nil views:views]]; 
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:kVConstraint options:0 metrics:nil views:views]]; 
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:kVConstraint2 options:0 metrics:nil views:views]]; 

我遊蕩,如果我可以在兩個垂直約束合併成一個?


是的,這是可以通過選項NSLayoutFormatAlignAllTop:

NSDictionary *views = NSDictionaryOfVariableBindings(button1, button2); 
NSString *const kHConstraint = @"H:|-[button1(==button2)]-[button2]-|"; 
NSString *const kVConstraint = @"V:|-[button1(==button2)]"; 
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:kHConstraint options:NSLayoutFormatAlignAllTop metrics:metrics views:views]]; 
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:kVConstraint options:0 metrics:metrics views:views]]; 

從大文章:@jrturton和here

回答

3
  1. 您需要關閉自動調整爲可以發現兩個按鈕:

    button1.translatesAutoresizingMaskIntoConstraints = NO; 
    button2.translatesAutoresizingMaskIntoConstraints = NO; 
    
  2. 您必須爲系統指定足夠的約束以確定每個子視圖的高度,寬度和位置。否則,默認框架是CGRectZero,它將不可見。

  3. 目前您的VFL字符串將button1的寬度設置爲與button2相等,但是如果兩個按鈕的大小均爲零,則仍然滿足約束條件。
  4. 您的約束沒有定位信息。

至少,我建議以下VFL語句來給你所需要的佈局:

@"V:|-[button1(==44)]" 

這使你的Button1 44的高度,並從它定位標準插圖superview的頂部。

@"V:|[button2(==44)]" 

這並不相同,以按鈕2.也可以通過在水平佈局對齊選項實現這一點,但是這會混淆事物的這個答案的目的。

@"|-[button1(==button2)]-[button2]-|" 

這使得按鈕的寬度相等,並且將它們從超視圖的邊緣插入。

我已經寫了關於VFL的一些長度here這將有助於你的理解。

+0

謝謝修復,並學到更多。肯定會閱讀你的文章 – Haagenti