User558
User558

Reputation: 1175

How to add a "Done" button with in the DatePicker Through StoryBoard?

In my app i want to display UIDatePicker when user click on button.and that date save into UITextFiled.I done this things. My problem is when date picker appears there is no done button,How can add that done button. Upto now i tried.

- (IBAction)pickerAction:(id)sender
{
datePicker.datePickerMode=UIDatePickerModeDate;
datePicker.hidden=NO;
datePicker.date=[NSDate date];
[datePicker addTarget:self action:@selector(TextTitle:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"M-d-yyyy"];
selectedDate.text=[df stringFromDate:datePicker.date];
} 

-(void)TextTitle:(id)sender
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"M-d-yyyy"];
selectedDate.text = [NSString stringWithFormat:@"%@",
                      [df stringFromDate:datePicker.date]];
 }

How can i add done button with this code. please help me.

Upvotes: 10

Views: 10325

Answers (5)

Soorej Babu
Soorej Babu

Reputation: 368

go to the link below.

http://www.iostute.com/2015/01/create-and-use-date-picker-uidatepicker.html

i think it'll help you.

Upvotes: 2

User558
User558

Reputation: 1175

Answer is

datePicker=[[UIDatePicker alloc]init];
datePicker.datePickerMode=UIDatePickerModeDate;
[TextField1 setInputView:datePicker];

UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[toolBar setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(ShowSelectedDate)];
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];

[TextField1 setInputAccessoryView:toolBar];

Upvotes: 12

Raj Tandel
Raj Tandel

Reputation: 359

Check this library. It is very easy to implement.

https://github.com/hackiftekhar/IQActionSheetPickerView

Upvotes: 2

Kamlesh Shingarakhiya
Kamlesh Shingarakhiya

Reputation: 2777

You can use UIView and in this UIView add your Datepicker and Done button. Done button create Action event and in this you will handle your UIView hide and show. Hope this helps

Upvotes: 1

Vatsal Raval
Vatsal Raval

Reputation: 313

I added a UIToolbar with a UIBarButtonItem for the 'done' button in my xib with the frame set so that it's not initially visible (y value equal to the height of the parent view).

Every time the user access the picker, I changed the frame (the y value) of the UIDatePicker and the UIToolbar with an animation so that it slides up along with the picker from the bottom of the screen similar to the keyboard.

Check out my code below.

- (IBAction)showPicker
{
    if(pickerVisible == NO)
    {
        // create the picker and add it to the view
        if(self.datePicker == nil) self.datePicker = [[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 460, 320, 216)] autorelease];
        [self.datePicker setMaximumDate:[NSDate date]];
        [self.datePicker setDatePickerMode:UIDatePickerModeDate];
        [self.datePicker setHidden:NO];
        [self.view addSubview:datePicker];

        // the UIToolbar is referenced 'using self.datePickerToolbar'
        [UIView beginAnimations:@"showDatepicker" context:nil];
        // animate for 0.3 secs.
        [UIView setAnimationDuration:0.3];

        CGRect datepickerToolbarFrame = self.datePickerToolbar.frame;
        datepickerToolbarFrame.origin.y -= (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
        self.datePickerToolbar.frame = datepickerToolbarFrame;

        CGRect datepickerFrame = self.datePicker.frame;
        datepickerFrame.origin.y -= (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
        self.datePicker.frame = datepickerFrame;

        [UIView commitAnimations];
        pickerVisible = YES;
    }
}

- (IBAction)done
{
    if(pickerVisible == YES)
    {
        [UIView beginAnimations:@"hideDatepicker" context:nil];
        [UIView setAnimationDuration:0.3];

        CGRect datepickerToolbarFrame = self.datePickerToolbar.frame;
        datepickerToolbarFrame.origin.y += (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
        self.datePickerToolbar.frame = datepickerToolbarFrame;

        CGRect datepickerFrame = self.datePicker.frame;
        datepickerFrame.origin.y += (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
        self.datePicker.frame = datepickerFrame;
        [UIView commitAnimations];

        // remove the picker after the animation is finished
        [self.datePicker performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.3];
    }
}

Upvotes: 2

Related Questions