How to add a done button over the pickerview?

15,190

Solution 1

Add your picker view to an action sheet. First of all make sure youre view controller impliments UIPickerViewDelegate and UIActionSheetDelegate.

Then add this method, and call it when you want to show your picker.

- (void)showPicker {    
    UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Pick Value"
                                                      delegate:self
                                             cancelButtonTitle:@"Done"
                                        destructiveButtonTitle:nil
                                             otherButtonTitles:nil];

    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,180,0,0)];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.showsSelectionIndicator = YES;    

    [menu addSubview:pickerView];
    [menu showInView:self.view.superview];

    //Change the height value in your CGRect to change the size of the actinsheet
    [menu setBounds:CGRectMake(0,0,320, 615)];

    [pickerView release]; 
    [menu release]; 
}

Solution 2

I think you can try to put the picker inside a view with a 'Done' button and present that view

Share:
15,190
Bharat Jagtap
Author by

Bharat Jagtap

I am an iPhone application developer and trainer working for bitCode Technologies Pvt Ltd and Aavid Software, Pune .

Updated on June 04, 2022

Comments

  • Bharat Jagtap
    Bharat Jagtap about 2 years

    I want to display a picker view and a done button above the picker view .

    I have a custom label which will become first responder . So when it becomes first responder I am displaying a picker view as the input view for my label. Now I want to have a done button on the picker to dismiss the picker view. I played with the inputAccesoryView property of UIPickerView but did not succeed . inputAccessoryView is possible with UITextView , not sure about the UIPickerView . Here is the link for how it can be done with UITextView

    UITextView with “Done” button and “Return” key?

    Has any body tried this , if someone knows how to do this for picker , please answer.

    Thank you all in advance !!

  • Bharat Jagtap
    Bharat Jagtap about 13 years
    yes that's the idea ,it shud work. The above link mentioned by Jhaliya gives the complete solution.
  • Bharat Jagtap
    Bharat Jagtap about 13 years
    Yes but that would be more complicated rather the link provide above by Jhaliya explains a simpler way . . . Thanks
  • Chris
    Chris over 10 years
    This is against Apples policy.
  • Sabobin
    Sabobin over 10 years
    Chris it may well be but that has nothing to do with the question above.