いくつかの異なるコントロールを持つビューを作成しようとしていますが、ビューを垂直方向にのみスクロールするようにします。私は自動レイアウトを使用しているので、手動でサイズを指定する必要はありません(私はそれを行うことができますが、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;
}
私は奇妙だと思うこのコードからいくつかの結果があります。 viewDidLayoutSubviews
メソッド(スーパコール後)でスクロールビューの高さを記録すると、すべての制約を適用する必要があります。高さは0を報告しています。非常に最初の制約のためにスーパービューの幅を報告することが期待されます(私はその制約内で優先度を指定してその幅を強制しようとしましたが、依然として動作しませんでした)。
私が理解しているように、UIScrollView
のコンテンツサイズはコンテンツの本来のサイズで設定する必要がありますが、それは起こっていないようです。
誰もが分からないことを知っていますか?
+1 lorem ipsum起源について) – Stas