2014-04-30 2 views
1

"컨테이너"UIView에 자식 UIView 요소의 동적 번호를 추가 할 때 UI 문제가 발생합니다. 기본적으로 컨테이너 뷰 (self.containerAView 아래)의 크기를 동적으로 조정하여 추가 된 모든 "하위"하위 뷰의 높이에 맞출 수 있어야합니다. 지금까지 프레임을 재설정하기 위해 시도한 모든 시도는 효과가 없습니다. 초기 크기가 300x200 (w x h) 인 초기 containerAView (UIView) 요소를로드하는 * .xib 파일에 초기 크기가 정의되어 있다는 점은 주목할 가치가 있습니다."컨테이너"의 동적 크기 조정 UIView

- (void)drawScreen { 
    // handle all screen presentation initialization here 
    // programatically create a dynamic number of child views to add to the container view 
    for (int i = 0; i < 3; i++) { 
     int childViewHeight = 60; 
     int childViewWidth = 300; 

     UIView *childView = [[UIView alloc] initWithFrame:CGRectMake(0, (i * childViewHeight) + (i * 10), childViewWidth, childViewHeight)]; 
    childView.backgroundColor = [UIColor blackColor]; 

    [self.containerAView addSubview:childView]; 
    } 

    // build a mapping dictionary for all child elements in the container view 
    NSMutableDictionary *containerASubViewDictionary = [[NSMutableDictionary alloc] init]; 
    for (int i = 0; i < [self.containerAView.subviews count]; i++) { 
     [containerASubViewDictionary setValue:self.containerAView.subviews[i] forKey:[NSString stringWithFormat:@"childView%d", i]]; 
    } 

    // WHAT TO ADD HERE TO HANDLE RESIZING self.containerAView height??? 
} 

컨테이너보기의 동적 크기 조정에 도움이되는 어떤 도움도 아직 해결 방법을 찾을 수 없었기 때문에 크게 감사하겠습니다.

답변

4

아마도 가장 최근의 접근 방식 일 수있는 방법 중 하나는 인터페이스 빌더의 containerview에 4 개의 자동 레이아웃 제약 조건을 추가하는 것입니다.

네 가지 제약 조건을 추가 : X = 0, Y = 0, 폭 = 300 & 높이 = 200 이제

을 강조 표시 한 다음 .H 파일로 드래그를 제어하는 ​​인터페이스 빌더의 폭을 제약 클릭 containerWidth와 같은 이름을 지정합니다. 높이와

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *containerWidth; 

를 반복합니다.

.m 파일에서 contstraints를 조작하여 drawScreen 메소드에서 높이와 너비를 조정할 수 있습니다.

self.containerWidth.constant = width; // your calculated new width 
self.containerHeight.constant = height; // your calculated new height 
+1

감사합니다.이 코드는 매우 잘 작동하며 코드에서 쉽게 제어 할 수 있습니다. UIView 컨테이너에 포함 된 모든 하위 뷰의 전체 높이를 쉽게 얻는 방법에 대한 제안 사항이 있습니까? –

0

컨테이너 프레임의 높이를 변경하려고 시도하는 것 같습니다. 프레임은 원점과 크기를가집니다. 다음 속성을 조작 해보십시오 :

self.containerView.frame.origin.x 
self.containerView.frame.origin.y 
self.containerView.frame.size.height 
self.containerView.frame.size.width