私は新しいプロジェクトを持っており、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
必要に応じてサイズが変更されます (圧縮抵抗がないため、テーブルが表示されなくてもかまいません)。
動作しないもの:AutoLayout
とChildControllers
次に、簡単にするために、使用することにしましたChildControllers
(カスタム UIFont セットアップを実行するには多くのアウトレットが必要です。残念なことに、XCode はまだそれらを処理できません!)。次の変更を加えました。
- HomeTopViewController クラス + XIB を作成しました。HomeTableViewController クラス + XIB を作成しました。
- すべてのビューをオリジンから正しい XIB にコピーしました。UIViewController 参照を追加し、コンセントを配線します。
- のルート コンテナは高さ200px
HomeTopViewController
に設定されています。 - 私のコンテナを
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
その後、他のアウトレットを作成し、初期フレーム/高さHomeTopViewController
をHomeTableViewController
UIViewController プロパティに格納する必要はありません。
質問
- 読み込み時に、すべての controller.view.frame プロパティのサイズが画面上に表示されますか? (3、5、または4インチ)
- どこかにサイズをハードコーディングせずにこれを解決するにはどうすればよいですか // 追加のアウトレットを作成しますか?