Note that there are some explanatory texts on larger screens.

plurals
  1. POPlacing UIViews one after another
    primarykey
    data
    text
    <p>I have a view controller which has an array of subviews. These subviews are custom UIViews to be placed one after another vertically. The subviews are not consistent in height; their height is based on the data used when instantiating them. Additionally, I want to support device rotations so that the subview's UILabels will expand horizontally in landscape mode and be shorter.</p> <p>I'm having lots of trouble accomplishing this in a good way. When the view controller is laying out it's subviews, I feel like I have no clean way of figuring out how tall each subview is (because I have instantiated them at this point, but not set their frames). Please refer to my code below to understand my frustration.</p> <p>I would really like it if the <strong><code>layoutSubviews</code></strong> method of the subviews were called before the <strong><code>viewWillLayoutSubviews</code></strong> method of the view controller, but that is not the case.</p> <p><strong><code>MyViewController.m</code></strong></p> <pre><code>.... - (void)initWithDataObjects:(NSArray*)dataObjects { _dataObjects = dataObjects; } - (void)viewDidLoad { mySubviews = [[NSMutableArray alloc] init]; for (DataObject* do in _dataObjects) { MyCustomView *customView = [[MyCustomView alloc] initWithDataObject:do]; [mySubviews addObject:customView]; [self.view addSubview:customView]; } } - (void)viewWillLayoutSubviews { int currentHeight = 0; for (MyCustomView *customView in mySubviews) { int subviewHeight = customView.frame.size.height; // PROBLEM: subviewHeight is 0 because the subview hasn't called layoutSubviews yet.. [customView setFrame:CGRectMake(0, currentHeight, self.view.frame.size.width, subviewHeight)]; currentHeight += subviewHeight; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload