View Height When Unhidden in a UIStackView?

I have several initially hidden UIPickers in a vertical stack view, where the user chooses one via a segmented controller or a table view elsewhere, and it is unhidden (code below). Visually it works fine - the correct picker appears. But I want to add a subview (a units label) to several of the pickerviews. When I immediately look at the frame of newView in the code below, the frame origin has values as well as the width, but the height is always zero as if the view is still totally collapsed in the stack view. What I need is the height of the picker view to calculate where to put the label subview before it appears.


But paradoxically (to me), the next time around when another picker view is chosen (without the segmented controller around so the picker will be bigger because of AutoLayout constraints), the newView frame for the previous picker appears WITH A HEIGHT and does not reflect the size of the just unhidden picker.


Does anyone know how to find the height of the expanded unhidden picker view before or immediately after it appears?


- (void)showPickerInStackView:(NSUInteger)newPickerIndex {
// hide them all first
        for (int i = 0; i < self.pickerStackView.arrangedSubviews.count; i++) {
            self.pickerStackView.arrangedSubviews[i].hidden = YES;
        }

        UIView *newView = self.pickerStackView.arrangedSubviews[newPickerIndex];
        newView.hidden = NO;
}

Replies

Just a hint: I would adjust it in viewDidLayout, to be sure all dimensions have been computed.


But as you show only a stampsized tiny fragment of your code, it is difficult to be sure of what to advise.

Unfortunately, even grabbing the pickerview instances and measuring them in viewDidLayoutSubviews every time I hid all of them and unhid one of the pickerviews (I didn't know that would necessarily happen - so thank you for the hint), the frame measurements of the visible pickerview was still showed the previously-sized frame and not what is actually visible.


For example, when a picker view starts with a height of 350, and then a selection from the segment control displays that same picker but the visible height is 310, in viewDidLayoutSubviews will stil lists that picker with a height of 350. When one of the other segments is chosen, the original picker now shows a height of 310 (what I expected it to be once everything was completed before) but the new segment has a height of 0.


Something happens with pickerviews after viewDidLayoutSubviews, and that's what I'm looking for. But since I only really have two sizes, I suppose I can hack something together (get the two heights from the wrong pickerview and use them somehow) and hope it doesn't break with the next iOS version.