몇 가지 다른 컨트롤이있는 뷰를 만들려고하지만 뷰를 세로 방향으로 만 스크롤하려고합니다. 나는 수동으로 크기를 지정하지 않으려 고 자동 레이아웃을 사용하고 있습니다. (내가 할 수있는 것은 알고 있지만, 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
의 콘텐츠 크기는 콘텐츠의 본질적인 크기로 설정해야하지만 그렇게되지는 않습니다.
누락 된 부분을 아는 사람이 있습니까?
+1 lorem ipsum origin의 경우) – Stas