2012-12-12 2 views
5

몇 가지 다른 컨트롤이있는 뷰를 만들려고하지만 뷰를 세로 방향으로 만 스크롤하려고합니다. 나는 수동으로 크기를 지정하지 않으려 고 자동 레이아웃을 사용하고 있습니다. (내가 할 수있는 것은 알고 있지만, UIScrollView의 릴리스 노트에 따라 이해할 수있는 것은 아닙니다).AutoLayout (iOS6)과 함께 UIScrollView를 어떻게 사용합니까?

따라서 .xib 파일이없는 간단한보기 컨트롤러를 만든 다음 init 메서드에 다음을 추가하면 내 레이블이 래핑되고 (내 레이블이 래핑 될 것임) 내 뷰가 세로로 스크롤됩니다. 그러나 이것은 사실이 아닙니다.

- (id)init { 
    self = [super init]; 
    if (self) { 

     UIScrollView *_scrollView = [UIScrollView new]; 
     [_scrollView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

     UILabel *label1 = [UILabel new]; 
     UILabel *label2 = [UILabel new]; 

     [label1 setTranslatesAutoresizingMaskIntoConstraints:NO]; 
     [label2 setTranslatesAutoresizingMaskIntoConstraints:NO]; 

     [label1 setNumberOfLines:0]; 
     [label2 setNumberOfLines:0]; 

     [label1 setPreferredMaxLayoutWidth:240]; 
     [label2 setPreferredMaxLayoutWidth:240]; 

     NSString *sampleText = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; 
     [label1 setText:sampleText]; 
     [label2 setText:sampleText]; 

     [self.view addSubview:_scrollView]; 
     [_scrollView addSubview:label1]; 
     [_scrollView addSubview:label2]; 

     NSArray *constraints; 
     constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]; 
     [self.view addConstraints:constraints]; 

     constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]; 
     [self.view addConstraints:constraints]; 

     constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label1]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1)]; 
     [_scrollView addConstraints:constraints]; 

     constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label2]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label2)]; 
     [_scrollView addConstraints:constraints]; 

     constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label1]-[label2]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1, label2)]; 
     [_scrollView addConstraints:constraints]; 
    } 

    return self; 
} 

이상한 것으로 생각되는이 코드의 결과는 여러 가지가 있습니다.메서드 (모든 수축이 적용되어야하는)에서 scrollviewheight를 기록하면 높이가 0으로보고됩니다. 너비가 예상 한 것과 다른 수를보고한다는 것이 이상한 것입니다. 매우 첫 번째 제약으로 인해 superview의 너비를보고 할 것으로 기대합니다 (나는 심지어 요구되는 우선 순위로 그 제약 내에서 너비를 강제적으로 시도했지만 여전히 작동하지 않았습니다).

제가 알고 있듯이 UIScrollView의 콘텐츠 크기는 콘텐츠의 본질적인 크기로 설정해야하지만 그렇게되지는 않습니다.

누락 된 부분을 아는 사람이 있습니까?

+0

+1 lorem ipsum origin의 경우) – Stas

답변

9

내 질문에 대한 답변을 얻으려는 Apple의 지원을 요청했습니다. 이 코드의 문제점은 UIScrollView에 포함 된 객체가 내부적으로 내용 크기를 결정할 수 있도록 위쪽과 아래쪽에 모두 고정되어야한다는 것입니다.

그들은 또한 나는 기본이 "초기화하기"여러 개체에 "새로운"키워드를 사용하는 것을 지적
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label1]-[label2]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1, label2)]; 
[_scrollView addConstraints:constraints]; 

이 : 그래서 다음 코드 줄을

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label1]-[label2]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1, label2)]; 
[_scrollView addConstraints:constraints]; 

은 변경해야 지정된 초기화 프로그램이 아닙니다. 그것이 내 특정한 문제와 관련이없는 동안 그들은 당신이 당신의 객체가 무효 상태가 될 수 있으므로 그렇게하지 말아야한다는 것을 지적했다.

+0

Adam은 새로운 방법에 대한 사과의 대답에 대해 더 구체적으로 설명 할 수 있습니까? 사용하지 말아야한다고 말했습니까? – Stas

+2

@Stas http://stackoverflow.com/questions/719877/use-of-alloc-init-instead-of-new-objective-c –

관련 문제