UIDatePicker with UIToolbar
Asked Answered
S

4

18

I'm trying to implement UIToolbar on UIDatepicker to dismiss it when touching "Done" button. But when I tap on the button, the datepicker is scrolling and the action button doesn't work.

Here is my code :

datepicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
[datepicker setDatePickerMode:UIDatePickerModeDate];
[datepicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];

UIToolbar *toolbar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,44)];
toolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismiss)];

[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
[datepicker addSubview:toolbar];

My datepicker :

UIDatePicker

Thank you for your help.

Stereoscopic answered 19/6, 2015 at 7:12 Comment(1)
have you implemented dismiss method ?Tyranny
E
29

If you use UITextField set

textField.inputAccessoryView = toolbar;
textField.inputView = datepicker;
Evelynneven answered 19/6, 2015 at 7:28 Comment(0)
U
3

You have to set inputAccessoryView and InputView to to your UITextField

txtField.inputAccessoryView = toolBar;
txtField.inputView = datePickerView;
Uzbek answered 19/6, 2015 at 7:30 Comment(0)
L
0
remove this line from your code:
[datepicker addSubview:toolbar];
and add dismiss method nd add 
txtCurrent.inputAccessoryView = toolbar; in didbeginEditing.

-(void)textFieldDidBeginEditing:(UITextField *)textField {

    txtCurrent = textField;
    [datePicker setHidden:NO];
    txtCurrent.inputAccessoryView = toolbar;
    if ([textField.text isEqualToString:@""]) {

        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        NSDate *eventDate = [NSDate date];
        [dateFormat setDateFormat:@"MM/dd/yyyy"];
        NSString *dateString = [dateFormat stringFromDate:eventDate];
        textField.text = [NSString stringWithFormat:@"%@",dateString];
        }
        [textField setInputView:datePicker];
}
-(void)dismiss{
    datePicker.hidden = YES;
    [self.view endEditing:YES];

}
Lennon answered 16/5, 2017 at 11:6 Comment(0)
S
0

Try this [self.view addSubview:toolbar];

picker = [[UIDatePicker alloc] init];
picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
picker.datePickerMode = UIDatePickerModeTime;

[picker addTarget:self action:@selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
//CGSize pickerSize = [picker sizeThatFits:CGSizeZero];
picker.frame =  CGRectMake(0.0, self.view.frame.size.height - 250, self.view.frame.size.width, 250);
picker.backgroundColor = [UIColor whiteColor];

toolbar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height - 294,self.view.frame.size.width,44)];
toolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIButton* infoButton = [UIButton buttonWithType: UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchDown];

UIBarButtonItem* doneButton =[[UIBarButtonItem alloc]initWithCustomView:infoButton];



[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];


[self.view addSubview:toolbar];


[self.view addSubview:picker];
Scheldt answered 23/8, 2017 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.