2014-10-28 50 views
1

我的子視圖控制器(UITableViewController)的viewDidLoad我在調用layoutIfNeeded後得到contentSize.Height。比我設置preferredContentSize與這些值。在我的容器(持有這個孩子)的viewDidLoad我還設置了preferredContentSize根據孩子的preferredContentSize。這適用於iOS 8,但不適用於iOS 7.獲取子視圖控制器的表視圖大小

我知道兒童視圖控制器的viewDidLoad在容器的viewDidLoad之後被調用。

如何獲得孩子的表視圖大小或我怎麼能強迫孩子視圖有佈局的容器有前它的子視圖?

回答

0

我結束了以下內容:

在我創建了一個單獨的方法,它採用的值的子視圖控制器表視圖。之後,我設置了首選內容大小。

public void setDocumentList(List<string> documentList){ 
    this.documentList = documentList; 

    this.TableView.ReloadData(); 

    // adjust size for popover 
    float height = TableView.ContentSize.Height; 
    if (this.View.Bounds.Height > 0) { 
     // limit the max size of the popover 
     height = Math.Min (this.View.Bounds.Height, height); 
    } 
    this.PreferredContentSize = new SizeF (320f, height); 
} 

在我的容器中我有一個類似的功能,但在這裏我只設置了首選的內容大小。

public void setDocumentList(List<string> documentList){ 
    documentListController.setDocumentList (documentList); 
    this.PreferredContentSize = documentListController.PreferredContentSize; 

    // some autolayout constraints ... 
} 

這類作品。我得到正確的值,但popover的值在開始時不正確。如果有人想知道這是C#語言。我仍然樂意提供更好的解決方案。

0

你可以嘗試發送佈局請求後,所有的意見都裝:

// In the container 
-(void) viewDidLoad { 

    // ... 

    [self.view performSelector:@selector(setNeedsLayout) withObject:nil afterDelay:0]; 

} 
+0

感謝您的答覆。我試過了,但contentSize在孩子中仍然是空的,所以容器得到零值。看起來,在iOS 7中,視圖控制器尚未準備好(未計算幀)。在'viewDidAppear'中,我可以查詢'contentSize',但這太遲了。 – testing 2014-10-28 11:54:57

+0

您是否嘗試使用'layoutSubviews'而不是'layoutIfNeeded'?如果不工作,你可能需要手動計算高度... – jjv360 2014-10-28 12:03:15

+0

現在我'layoutSubviews'試圖在'viewDidLoad'我的孩子視圖控制器。我仍然得到相同的結果(零值)。我想我會嘗試手動計算高度。謝謝! – testing 2014-10-28 12:16:26

0

您可以通過撥打孩子的viewDidLoad中的父母,並迫使實現代碼如下佈局加載孩子的tableView。

// Parent.m 
[Child view]; 
[self layoutParentViewsOrDoSomething]; 

// Child.m 

-(void)viewDidLoad{ 
[super viewDidLoad]; 
[self layoutSubviews]; 
// or [tableView reloadData]... 
} 
相關問題