Using IB, I have set up a UIVPicker in a Container View's associated UIViewController. The Container's height is controlled by two auto-layout constraints, one which sets the height to zero, one which sets it the same as the height of its superview. I change the priority of one of the constraints to toggle active/inactive. In IB, the priorities are set so the picker's initial height is zero (full-height constraint priority 750; zero-height constraint priority 751).
When I make the full-height constraint active, I can see it taking effect: the UIPickerView has a colored background. I can see the background expand to the height of the superview. Here's the weird part: The UIPickerView's own subviews (picker titles, selection bars, and so on) are not resizing along with the picker's own background. I can see them all bunched up at the top of the picker display in the top 30 pixels or so. I have verified it is the picker's background I am seeing expand when I toggle the constraints using XCode's ability to display layers when debugging.
If I set the zero-height constraint priority to 749 in IB to allow the full-height constraint to be active and then 'hide' the pickerView after it appears by putting the following in viewDidAppear...
self.pickerContainerViewHeightConstraintZeroHeight.priority = 751;[self.pickerContainerView layoutIfNeeded];
...then the picker contents display correctly. (This is a diagnostic-only solution because the user sees the picker flash and then disappear.) I can then hide and display the picker at will and it continues to look as it should. If I put the same code in viewDidLayoutSubviews or viewWillAppear to try to turn this into a 'real' solutionn, the picker is messed up when I display it.
The following actually does work, but it's really ugly... Set the zero-height constraint priority to 749 in IB to allow the full-height constraint to be active and then...
- (void )viewWillAppear:{ self.pickerContainerView.hidden = YES; }- (void )viewDidAppear:{ self.pickerContainerViewHeightConstraintZeroHeight.priority = 751; [self.pickerContainerView layoutIfNeeded]; self.pickerView.hidden = NO; }
...which amounts to letting the view be full height when it's built, hiding it using the hidden attribute so the user doesn't see it at first, changing its height to zero, 'showing' it (which the user won't see because the height is now zero) so that when I next change the height to full height, the picker shows correctly.
But there has to be a better way. How do I start with the picker height = 0 and still get it have the correct appearance when it is shown by changing constraints?