3

私は新しいプロジェクトを持っており、iOS 5 以降の機能を使用できるので、両方の機能AutoLayoutと UIViewControllerchildControllers機能を使用することにしました。

私が作成しようとしている画面は単純です: 1. 上部領域には多くの「複雑な」コントロールがあります。2. 下部の領域は、結果を視覚化するためのテーブル ビューです。

私が欲しいもの

コントロール領域に必要なすべての場所 (つまり、Interface Builder に指定した場所) を配置したいと考えています。TableView は必要に応じて縮小されます。

問題

XIB からロードされた場合、UIViewController.viewフレームのサイズは常に 0x568 です。0x200 と予想されていました (Interface Builder で構成したサイズが 200 の場合)。

機能するもの:AutoLayout

すべての要素を含む XIB を作成しました。「ルートビュー」でアウトレットを行い、アウトレットとすべてのビューコンテナを行いました。

次に、viewDidLoad メソッドにコンテナーを追加し、制約を構成しました。

-(void) setupConstraints
{
    NSMutableArray *constraints = [NSMutableArray arrayWithCapacity:1];

    // Views dictionary.
    UIView *topView = self.topView;
    UIView *table   = self.tableTracks;
    NSDictionary *views = NSDictionaryOfVariableBindings(topView, table);

    // Views metrics.
    NSNumber *topViewHeight = [NSNumber numberWithFloat:self.topView.frame.size.height];
    NSDictionary *metrics = NSDictionaryOfVariableBindings(topViewHeight);

    // Pin the topView to the top edge of the container.
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topView(==topViewHeight)][table]|" options:0 metrics:metrics views:views]];

    // Pin the topView edges to both sides of the container.
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[topView]|" options:0 metrics:nil views:views]];

    // Pin the table edges to both sides of the container.
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[table]|" options:0 metrics:nil views:views]];

    [self.view addConstraints:constraints];
}

topViewこれにより、Interface Builder を使用してビューのサイズを簡単に変更でき、TableView必要に応じてサイズが変更されます (圧縮抵抗がないため、テーブルが表示されなくてもかまいません)。

動作しないもの:AutoLayoutChildControllers

次に、簡単にするために、使用することにしましたChildControllers(カスタム UIFont セットアップを実行するには多くのアウトレットが必要です。残念なことに、XCode はまだそれらを処理できません!)。次の変更を加えました。

  1. HomeTopViewController クラス + XIB を作成しました。HomeTableViewController クラス + XIB を作成しました。
  2. すべてのビューをオリジンから正しい XIB にコピーしました。UIViewController 参照を追加し、コンセントを配線します。
  3. のルート コンテナは高さ200pxHomeTopViewControllerに設定されています。
  4. 私のコンテナを view子供のコントローラのアウトレットに配線しました。

次に、セットアップ コードを次のように更新しました。

-(void) _addChildViewControllersAndSetupConstraints {
    // Get required metrics variables
    NSNumber *topViewHeight = [NSNumber numberWithFloat:self.topViewController.view.frame.size.height];

    CFShow(self.topViewController.view);
    // log is : <UIView: 0xa209c20; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0xa2097e0>>
    NSLog(@"self.topViewController.view.frame.size.height = %f", self.topViewController.view.frame.size.height);


    // Informs controllers the ADD operation started
    [self addChildViewController:self.topViewController];
    [self addChildViewController:self.tableViewController];

    // Add view to the view's hierarchy
    self.topViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
    self.tableViewController.view.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:self.topViewController.view];
    [self.view addSubview:self.tableViewController.view];


    // Setup constraints
    NSMutableArray *constraints = [NSMutableArray arrayWithCapacity:1];

    // Views dictionary
    UIView *topView = self.topViewController.view;
    UIView *table   = self.tableViewController.view;

    NSDictionary *views = NSDictionaryOfVariableBindings(topView, table);

    // Views metrics dictionary
    NSDictionary *metrics = NSDictionaryOfVariableBindings(topViewHeight);

    // Pin the topView to the top edge of the container.
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topView(==topViewHeight)][table]|" options:0 metrics:metrics views:views]];

    // Pin the topView edges to both sides of the container.
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[topView]|" options:0 metrics:nil views:views]];

    // Pin the table edges to both sides of the container.
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[table]|" options:0 metrics:nil views:views]];

    // Adds constraints
    [self.view addConstraints:constraints];


    // Informs controllers the ADD operation ended
    [self.topViewController didMoveToParentViewController:self];
    [self.tableViewController didMoveToParentViewController:self];
}

問題

HomeTopViewController行内の UIView コンテナーのサイズをどのように変更しても、 200px を期待したときにCFShow(self.topViewController.view);常に表示されます。frame = (0 0; 320 568)

レイアウト サイズを「フリーフォーム」または「なし」に設定しました。

できるだけ避けたいこと

Viewその後、他のアウトレットを作成し、初期フレーム/高さHomeTopViewControllerHomeTableViewControllerUIViewController プロパティに格納する必要はありません。

質問

  1. 読み込み時に、すべての controller.view.frame プロパティのサイズが画面上に表示されますか? (3、5、または4インチ)
  2. どこかにサイズをハードコーディングせずにこれを解決するにはどうすればよいですか // 追加のアウトレットを作成しますか?
4

1 に答える 1

0

ストーリーボードを使用すると、これがより簡単になります。コンテナー ビューをメイン コントローラーのビューに追加して、好きなようにサイズを変更できます。正しいサイズに設定されたこれらのコンテナー ビューに埋め込まれたビュー コントローラーを自動的に取得します。これらのコンテナー ビュー (オブジェクト リストの通常の UIView の隣) は、xibs ではなく、ストーリーボードからのみ使用できます。これにはコードはほとんど必要なく、手動で制約を追加する必要はおそらくありません。

于 2013-01-03T20:45:39.600 に答える