I have a navigation-based app and say my root controller A causes a viewController B to be pushed, which has a UITableView with a few cells with UITextFields.
The first 2 UITextFields have each, as their inputView, a UIDatePicker and a UIPickerView, respectively. Also, the UIPickerView has 4 components whose UIViews are returned through
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 37)];
label.text = [NSString stringWithFormat:@"%@%i", ((component == 3) ? @"." : @""), row];
label.textAlignment = UITextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:24];
label.backgroundColor = [UIColor clearColor];
[label autorelease];
return label;
}
In vertical orientation, the UIPickerView looks like this:
What I've noticed is the following:
CASE 1 - CORRECT
The picker frame is correctly stretched to landscape mode and the component views are still the correct size and centered:
BUT
CASE 2 - INCORRECT
In this case, the UIPickerView's components get messed up:
Any thoughts?
So I figured this much: Don't know why, but essentially a call to -setNeedsLayout
is missing for the input view when the device rotates.
UIDatePicker (inputView = UIDatePicker) results in a picker that automatically (re) lays itself out to the right orientation, but UIPickerView (inputView = UIPickerView) does not.
So it seems like UIPickerView might be not registering for orientation changes or something of the sort. Which I solved by registering it manually. That is, in my setup code I add:
[[NSNotificationCenter defaultCenter] addObserver:weightPicker
selector:@selector(setNeedsLayout)
name:UIDeviceOrientationDidChangeNotification
object:nil];
And now the picker behaves just fine.
Also, remember to deregister with the NSNotificationCenter before the textField gets deallocated.