를 (명시 적으로 크기 제한을 추가해야합니다), 당신의 UIScrollView
의 contentSize
여기의 파단의 intrinsicContentSize
, 당신의 SinglePostView
에 의해 구동된다.
은 UIView
의 서브 클래스이고, intrinsicContentSize
은 자체적으로 고려할 때 항상 CGSizeZero
입니다. 당신이해야 할 일은 자신의 SinglePostView
의 intrinsicContentSize
을 서브 뷰의 intrinsicContentSize
에 의존하도록하는 것입니다. 그런 다음
때문에
SinglePostView
이
UILabels
을, 그리고 당신의 파단
UILabel
의
intrinsicContentSize
는 내용을 표시하는 데 필요한 최소 크기는 당신의
SinglePostView
'때문에의
intrinsicContentSize
의 파단
intrinsicContentSizes
의 합과 동일 할 것이다, 그 세 라벨 모두의 내용을 표시하는 데 필요한 총 크기입니다.
다음은 수행 방법입니다.
1 단계 : 부분적처럼, 당신은 시스템에 의해 자동으로 설정된 모든 제약 조건을 제거해야,
우선 자동으로 설정 제약 조건을 제거.
어떤 제약 스토리 보드 또는 XIB에서 설정 (또는 심지어 이들 중 하나가없는)가없는 당신을 가정하면, 단지하십시오 지금
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
singlePost.translatesAutoresizingMaskIntoConstraints = NO;
titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
contentLabel.translatesAutoresizingMaskIntoConstraints = NO;
당신은 분명 슬레이트를 가지고 수행 할 수 있습니다 자신의 제약 조건 설정을 시작하십시오.
2 단계 :
NSDictionary *views = NSDictionaryOfVariableBindings(scrollView, singlePost, titleLabel, subtitleLabel, contentLabel);
을 그럼 당신은 이미 한도 같이의가을 제한하자, 상기 scrollView
먼저 구속, 당신이 그랬던 것처럼 자동 레이아웃을 사용하려면, 뷰 참조 사전,의를 만들 수 있습니다
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[scrollView]-0-|" options:0 metrics:0 views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[scrollView]-0-|" options:0 metrics:0 views:views]];
3 단계 : 다음를 밀어 SinglePostView
구속 뷰 스크롤은 수퍼 사이즈 될 이 단계 53,333,210의 contentSize
명확로, 당신은 UIScrollView
과 subviews
사이에 모든 제약 조건이 실제로 bounds
실제되지는 UIScrollView
의 contentSize
변경됩니다 것을 이해합니다. 예를 들어, UIImageView
을 부모의 경계 인 UIScrollView
으로 제한 한 다음 UIScrollView
크기의 두 배 이미지를 UIImageView
안에 넣으면 이미지 크기가 줄어들지 않고 해당 이미지의 크기를 취하는 UIImageView
및 UIScrollView
안에서 스크롤 할 수있게됩니다.
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[singlePost]-0-|" options:0 metrics:0 views:views]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[singlePost]-0-|" options:0 metrics:0 views:views]];
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:singlePost
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:scrollView
attribute:NSLayoutAttributeWidth
multiplier:1.0f
constant:0.0f]];
처음 두 제약은 매우 명백하다 : 그래서 여기
는 여기에서 설정해야 할 것입니다. 그러나 세 번째 것은 여기에 있습니다. 왜냐하면 여러분의 UILabels
이 내용을 올바르게 표시하고 읽을 수 있기를 원할 것입니다. 아마 여러 줄을 사용하고 가로로 스크롤하지 않고 세로로 스크롤하기를 원할 것입니다. 그래서 SinglePostView
의 width
을 scrollView
과 동일하게 설정했습니다. 이렇게하면 scrollView
의 contentSize.width
이 bounds.width
이상이되지 않게됩니다.
4 단계 : SinglePostView
마지막 네 번째 단계의 당신의 UILabels
에 "푸시"경계를 구속, 당신은 지금 그들로부터 intrinsicContentSize
를 얻을 수 있도록, 당신의 SinglePostView
의 파단에 대한 제한을 설정해야합니다. 여기
당신이 그것을하는 방법이다 (간단한 구현, 여백, 세로로 하나씩 라벨) :
[singlePost addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[titleLabel]-0-|" options:0 metrics:0 views:views]];
[singlePost addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[subtitleLabel]-0-|" options:0 metrics:0 views:views]];
[singlePost addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[contentLabel]-0-|" options:0 metrics:0 views:views]];
[singlePost addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[titleLabel]-0-[subtitleLabel]-[contentLabel]-0-|" options:0 metrics:0 views:views]];
그리고 당신은 수행해야 그와
.
마지막 조언 하나만하면 이러한 종류의 작업을 수행하려면 UIStoryboard
을 조사해야합니다. 훨씬 간단하고 시각적입니다. 당신이 원하는 경우에이 도움이
희망,
P.S는 : 나는 약간의 시간이 걸릴 모두 UIStoryboard
과 Github에서의 Visual Format Language
을 사용하여 프로젝트를 푸시 할 수 있습니다. 네가 필요하면 말해줘.
행운을 빈다.
알려주세요. 훌륭한 설명에 감사드립니다. 3 단계 및 contentsize의 너비가 한정되어 있는지 확인하려면 누락 된 링크가 있습니다. – Msencenb
@Msencenb iPhone 4S에서 scrollView의 높이가 같은 폭으로 축소되는 경우 어떻게해야합니까? 나는 이것과 같은 구조를 가지며 5 개의 동일한 크기의 UIView가 ScrollView에 있습니다. 사용자는 가로로만 스크롤 할 수 있습니다. 그것은 iPhone 5, iPhone 6, iPhone 6 plus에서 완벽하게 작동합니다. 그러나, scrollView의 높이가 줄어들 때 (의도적으로 콘텐츠 포깅 및 콘텐츠 저항으로 인해) iPhone 4에서 UIView가 잘 맞지 않습니다. 나는 그림을보기 위해 수직으로 내려야 만한다. 그러나 그것은 내가 원하지 않는다. scrollView content size 'height를 확인했는데, iPhone 5에서와 동일합니다. –
uiscrollviews에 대한 intrinsicContentSize를 참조하는 명성입니다. 나는 instrinsicContentSize 오버라이드가 필요한 커스텀 UI를 가지고 있기 때문에 그것을 잃어 가고 있다고 생각했다. –