1

UIScrollView を使用しているときに、自動レイアウトとコンテンツ インセットに関する奇妙な動作に気付きました。たとえば、次のコードを見てください。

- (void) loadView
{
  UIScrollView * scroller = [[UIScrollView alloc] init];
  scroller.backgroundColor = [UIColor orangeColor];

  UILabel * label = [[UILabel alloc] init];
  label.backgroundColor = [UIColor blueColor];
  label.translatesAutoresizingMaskIntoConstraints = NO;
  [scroller addSubview:label];

  [scroller addConstraint: [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:scroller attribute:NSLayoutAttributeLeft multiplier:1.0 constant:40]];
  [scroller addConstraint: [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:scroller attribute:NSLayoutAttributeTop multiplier:1.0 constant:20]];
  [scroller addConstraint: [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:240]];
  [scroller addConstraint: [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:44]];

  self.view = scroller;
  scroller.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);  //this is the game changer
}

コンテンツ インセットを変更する最後の行がない場合、UILabel は予想どおり (40, 20) になります。コンテンツ インセットを変更すると、ラベルは (40, 64) ではなく (40, 108) に移動しました。上部のコンテンツ インセットがなぜか 2 倍になっています。UILabel が set frame で作成され、これらの制約なしで次のように作成された場合:

- (void) loadView
{
  UIScrollView * scroller = [[UIScrollView alloc] init];
  scroller.backgroundColor = [UIColor orangeColor];

  CGRect frame = CGRectMake(40, 20, 240, 44);
  UILabel * label = [[UILabel alloc] initWithFrame:frame];
  label.backgroundColor = [UIColor blueColor];
  [scroller addSubview:label];

  self.view = scroller;
  scroller.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
}

その後、コンテンツのインセットに応じて、期待どおりに (40, 20) および (40, 64) 動作します。

ドキュメントを確認し、いくつかのスイッチを切り替えてみましたが、まだ理由がわかりません。

ところで、これは IOS(6) シミュレータの結果であり、まだデバイスで試していません。

PS Things は、新しいステータス バーの動作の違いを除いて、IOS7 のシミュレーターで期待どおりに表示されます。

4

0 に答える 0