2013-12-18 1 views
2

Visual 형식 언어를 배우려고합니다.이 경우보기의 맨 위에 서로 옆에 두 개의 단추가 있어야합니다.iOS VFL 다음에 두 개의 버튼이 표시됩니다.

이것은 내 코드이지만 아무것도 표시되지 않습니까?

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]]; 

내가 하나에 두 개의 수직 제약을 결합 할 수 있다면 내가 방황하고이 지금 내 코드입니다 대답에

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]; 

감사합니다?


예이 옵션 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)]" 

이 당신의 단추 1 (44)의 높이를 제공을하고,에서 그것을 표준 인 세트를 배치 슈퍼 뷰의 맨.

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

이것은 또한 수평 레이아웃의 정렬 옵션하여이를 달성 할 수있는 버튼을 2로 동일하지 않습니다,하지만이 답변의 목적을 위해 일을 혼동됩니다.

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

이렇게하면 버튼의 너비가 동일 해지며 수퍼 뷰 가장자리에서 단추가 삽입됩니다.

나는 VFL에 대해 약간의 길이 here로 작성하여 귀하의 이해에 도움이 될 것입니다.

+0

고마워요. 좀 더 배웠습니다. 분명히 당신의 기사를 읽을 것입니다. – Haagenti

관련 문제