Search code examples
iosuitableviewuipopovercontrolleruicontainerview

Get table view size of child view controller


In viewDidLoad of my child view controller (UITableViewController) I'm getting the contentSize.Height after calling layoutIfNeeded. Than I set the preferredContentSize with these values. In viewDidLoad of my container (which holds this child) I also set the preferredContentSize based on the child's preferredContentSize. This works on iOS 8 but not on iOS 7.

I know that the viewDidLoad of the child view controller is called after the viewDidLoad of the container.

How do I get the table view size of the child or how can I force that the child view has layout its subviews before the container has?


Solution

  • I ended up with the following:

    In the child view controller I created a separate method which takes the values for the table view. Afterwards I set the preferred content size.

    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);
    }
    

    In my container I have a similar function, but here I only set the preferred content size.

    public void setDocumentList(List<string> documentList){
        documentListController.setDocumentList (documentList);
        this.PreferredContentSize = documentListController.PreferredContentSize;
    
        // some autolayout constraints ...
    }
    

    This kind of works. I get the correct values, but the values of the popover are not correct in the beginning. If someone is wondering this is the C# language. I'm still open for better solutions.