2012-12-12 6 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; 
} 

私は奇妙だと思うこのコードからいくつかの結果があります。 viewDidLayoutSubviewsメソッド(スーパコール後)でスクロールビューの高さを記録すると、すべての制約を適用する必要があります。高さは0を報告しています。非常に最初の制約のためにスーパービューの幅を報告することが期待されます(私はその制約内で優先度を指定してその幅を強制しようとしましたが、依然として動作しませんでした)。

私が理解しているように、UIScrollViewのコンテンツサイズはコンテンツの本来のサイズで設定する必要がありますが、それは起こっていないようです。

誰もが分からないことを知っていますか?

+0

+1 lorem ipsum起源について) – Stas

答えて

9

Appleのサポートに連絡して質問に答えました。このコードの問題点は、UIScrollViewに含まれるオブジェクトを、その本質的なコンテンツサイズを決定するために上下に垂直に固定する必要があることです。彼らはまた、基本的な「initが」ここで私はいくつかのオブジェクト上の「新しい」キーワードを使用していたことを指摘し

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

アダムは、新しい方法についてのアップルの答えをより具体的にすることができますか?使用を避けるべきだと言いましたか? – Stas

+2

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

関連する問題