Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C implementing a UIPickerView with a "Done" button

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

like image 416
user1051935 Avatar asked May 22 '12 14:05

user1051935


1 Answers

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;
    }];
}
like image 173
Paul Hunter Avatar answered Sep 21 '22 10:09

Paul Hunter