簡體   English   中英

UIViewController viewDidLoad

[英]UIViewController viewDidLoad

我有一個新項目,我可以使用iOS 5+功能,所以我選擇使用AutoLayout和UIViewController childControllers功能。

我想要創建的屏幕很簡單:1。頂部區域有很多“復雜”控件。 2.底部區域是用於可視化結果的tableview。

我想要的是

我希望控件區域占據所有必要的位置(即我給Interface Builder的地方)。 TableView將根據需要縮小。

問題

從XIB加載時, UIViewController.view框架的大小始終為0x568。 我被期待0x200(如果200是我在Interface Builder中配置的大小)。

什么有效:使用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];
}

有了這個,我可以使用Interface Builder簡單地調整topView視圖的大小,並且TableView將根據需要調整大小(沒有壓縮阻力,如果我們看不到表,我不在乎)。

什么AutoLayout :使用AutoLayoutChildControllers

然后為了簡單起見,我選擇使用ChildControllers (執行自定義UIFont設置所需的很多插座,太糟糕的XCode還無法處理它們!)。 我做了以下修改:

  1. 創建了HomeTopViewController類+ XIB。 創建了HomeTableViewController類+ XIB。
  2. 將所有視圖從原點復制到正確的XIB。 添加UIViewController引用+連接出口。
  3. HomeTopViewController的根容器配置為200px高度。
  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容器的行CFShow(self.topViewController.view); 當我預期200px時,總會給我frame = (0 0; 320 568)

我確實將布局大小配置為“自由格式”或“無”。

我想盡可能避免的

我不想創建其他插座然后在HomeTopViewControllerHomeTableViewControllerView並將初始幀/高度存儲到UIViewController屬性中。

問題

  1. 在加載時,屏幕上是否都調整了controller.view.frame屬性的大小? (是3,5或4英寸)
  2. 如何在不創建額外插座的情況下解決這個問題來解決這個問題?

如果您使用故事板,這將更容易。 您可以向主控制器的視圖添加容器視圖,並根據需要調整它們的大小,並自動獲取嵌入在這些容器視圖中的視圖控制器,這些視圖控制器設置為正確的大小。 這些容器視圖(在對象列表中的常規UIView旁邊)僅可從故事板中獲得,而不是xib。 這幾乎不需要代碼,您可能不必手動添加任何約束。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM