Objective C implementing a UIPickerView with a "Done" button

user1051935 picture user1051935 · May 22, 2012 · Viewed 33.2k times · Source

I am trying to implement a "Done" button in a UIPickerView Similar to the one under this link

I looked in the class reference but I couldn t find it

Thanks

Answer

Paul Hunter picture Paul Hunter · May 22, 2012

The easiest way to do it is to model it in Interface Builder. It is a UIView containing a UIToolbar and a UIPickerView.

enter image description here

Then create an outlet for the UIView and connect it.

enter image description here

If you then have a UITextField you can assign your custom view to its inputView property.

[self.textField setInputView:self.customPicker];

Alternatively you can add the picker to your main view...

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.customPicker.frame = CGRectMake(0, CGRectGetMaxY(self.view.frame), CGRectGetWidth(self.customPicker.frame), CGRectGetHeight(self.customPicker.frame));
    [self.view addSubview:self.customPicker];
}

... and then use this method to show or hide the picker.

- (void)setPickerHidden:(BOOL)hidden
{
    CGAffineTransform transform = hidden ? CGAffineTransformIdentity : CGAffineTransformMakeTranslation(0, -CGRectGetHeight(self.customPicker.frame));

    [UIView animateWithDuration:0.3 animations:^{
        self.customPicker.transform = transform;
    }];
}