I have written the following code in the viewDidLoad
method:
categoryPickerView=[[UIPickerView alloc]init];
categoryPickerView.alpha = 0;
[self.view addSubview:categoryPickerView];
categoryPickerView.delegate=self;
categoryPickerView.tag=1;
and called this method to hide picker view
- (IBAction)hidePickerView:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, 200);
categoryPickerView.transform = transfrom;
categoryPickerView.alpha = categoryPickerView.alpha * (-1) + 1;
[UIView commitAnimations];
}
My problem is that I want to display a "Done" button on a picker view and the picker view should hide on button click.
Copy-pasteable solution!
This works in iOS 10 (and probably other versions too) I wrote this code on 9/4/2017.
I needed to combine other answers to get what I wanted, which was a
Cancel
button,Done
button, and a picker view that would show/hide on the tap of a button. Ignore the fact that my screenshot only has one item in the picker.. I only have one item in my array. I've tested multiple items, it works fine.Disclaimer! I have tested and used this code. I generalized the names in my example, so I apologize in advance if I missed one and they property names don't line up.
In order to have a pickerView that appears on the tap of a button, you need to create a dummyTextField (of type
UITextField
anywhere in your view controller. This is becauseUIPickerView
's are designed to work with UITextFields. To mimic showing and hiding of the picker, your button taps need to call the dummyTextField'sbecomeFirstResponder
andresignFirstResponder
methods. (examples are below)Step one
Create a custom data source class. MyObj is the class you want to have items show up in the picker for.
In .h:
In .m:
Step two In your view controller, import custom data source, delegate, and set properties: (You must include the text field delegate!)
Step three
In your
viewDidLoad
, call[self setupPicker];
(you'll create this method next)Step four
Create
setupPicker
Step five
Create
configurePickerSubviews
Step six
Configure the
PickerDataSource
so that it is fetching the data you want to display from your source array.Step 7
Click
Run
!You can create view and add toolbar with "Done" button and UIPickerView as subviews
You need to use the UIToolbar as the accessory view: Try with this:
Try this.
http://technopote.com/how-to-make-multiple-uipickerview-in-a-single-view/
You can use this code,
and set button action method for
changeDateFromLabel: