2014-07-18 1 views
0

수퍼 뷰 (내 경우에는 UITableViewCell) 내에 가변 개수의 레이블을 세로 가운데에 배치 할 수 있습니까?셀/뷰에서 세로로 가변 수의 UILabel 중심 지정

내 앱의 특정보기에서 데이터베이스의 데이터를 표시하고 싶습니다. 데이터의 크기는 0에서 3까지이며, 뷰에는 해당 레이블이 0에서 3 개 있습니다. 라벨이 하나있는 경우 셀의 수직 중심선에 나타납니다. 레이블이 두 개인 경우 셀의 세로 중심선 위와 아래에서 동일한 거리로 각각 표시해야합니다. 세 개의 레이블이있는 경우 하나는 수직 중심선에 나타나야하고 다른 두 개는 중심선 위와 아래에 동일한 거리가 있어야합니다.

시각 장애가 발생하기를 바랍니다. 이 비주얼의 두 번째 예제에서는 레이블을 나타내는 두 개의 눈금이 세 번째 예제의 위쪽 및 아래쪽 레이블과 같은 거리만큼 떨어져있는 것처럼 보입니다.하지만 실제 위치에서는 약간 더 가깝습니다. 그보다 더 복잡하지만 복잡한 UI 요소를 모델링 할 때 일반 텍스트의 한계가 있습니다.

********************************************************************************** 

- - 


    - 
- These two should be slightly closer to center than shown here. 
    - 


    - 
- - 
    - 

********************************************************************************** 

내가 할 수있는 한 많은 제약을 가하는 시도를 해봤지만 이런 식으로 처리 할 방법을 찾지 못하는 것 같습니다.

저는 웹 프로그래밍 배경에서 부모 컨테이너에서 수직으로 중심이 정렬되지 않은 가변 높이 목록으로 설명 하겠지만 그 내용을 다시 만드는 방법을 모르겠습니다.

+0

plz는 cellForRowAtIndexPath 메소드 코드를 공유합니다. 자동 레이아웃만으로는이 경우를 처리 할 수 ​​없습니다. –

답변

0

containerView이 셀의 레이블을 처리하는 셀 내의 UIView라고 가정하면 아래 코드가 원하는대로 처리 할 것이라고 가정합니다.

NSLayoutConstraint* containerCenterXConstraint = 
    [NSLayoutConstraint constraintWithItem:containerView 
           attribute:NSLayoutAttributeCenterX 
           relatedBy:NSLayoutRelationEqual 
            toItem:containerView.superview 
           attribute:NSLayoutAttributeCenterX 
           multiplier:1 
            constant:0]; 

NSLayoutConstraint* containerCenterYConstraint = 
    [NSLayoutConstraint constraintWithItem:containerView 
           attribute:NSLayoutAttributeCenterY 
           relatedBy:NSLayoutRelationEqual 
            toItem:containerView.superview 
           attribute:NSLayoutAttributeCenterY 
           multiplier:1 
            constant:0]; 

[containerView.superview addConstraints:@[containerCenterXConstraint, containerCenterYConstraint]]; 


[containerView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
UILabel* previousLabel = nil; 
for (int i = 0; i < numberOfElements; i++) { 
    UILabel* label = [[UILabel alloc] init]; 
    [label setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    label.text = <init label with data>; 
    [label sizeToFit]; 
    [containerView addSubview:label]; 
    NSArray* constraints; 
    NSDictionary* viewsDict; 
    NSDictionary* metricsDict = @{@"labelHeight": @(CGRectGetHeight(label.frame))}; 
    if (i == 0 && numberOfElements == 1) { 
     viewsDict = NSDictionaryOfVariableBindings(label); 
     constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label(labelHeight)]|" 
                       options:0 
                       metrics:metricsDict 
                        views:viewsDict]; 

    } else if (i == 0 && numberOfElements > 1) { 
     viewsDict = NSDictionaryOfVariableBindings(label); 
     constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label(labelHeight)]" 
                   options:0 
                   metrics:metricsDict 
                   views:viewsDict]; 
    } else if (i == (numberOfElements - 1) && numberOfElements > 1) { 
     viewsDict = NSDictionaryOfVariableBindings(label); 
     constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[label(labelHeight)]|" 
                   options:0 
                   metrics:metricsDict 
                   views:viewsDict]; 


    } 

    if (i > 0 && numberOfElements > 1) { 
     viewsDict = NSDictionaryOfVariableBindings(previousLabel, label); 
     constraints = [[NSLayoutConstraint constraintsWithVisualFormat:@"V:[previousLabel][label(labelHeight)]" 
                   options:0 
                   metrics:metricsDict 
                   views:viewsDict] arrayByAddingObjectsFromArray:constraints]; 
    } 
    [containerView addConstraints:constraints]; 
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[label]|" 
                  options:0 
                  metrics:nil 
                  views:viewsDict]; 
    [containerView addConstraints:constraints]; 
    previousLabel = label; 
}