I pop up a DatePicker with the following. Now I'm trying to add a Done button at the top of the pop up frame.
-(IBAction) contactBDayDatePicker{
NSLog(@"contactBDayDatePicker");
pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDate;
if (self.pickerView.superview == nil){
[self.view.window addSubview: self.pickerView];
// size up the picker view to our screen and compute the start/end frame origin for our slide up animation
//
// compute the start frame
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
NSLog(@"screenRect %@",NSStringFromCGRect(screenRect));
CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];
NSLog(@"pickerSize %@",NSStringFromCGSize(pickerSize));
CGRect startRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height,
pickerSize.width, pickerSize.height);
self.pickerView.frame = startRect;
NSLog(@"pickerView.frame %@",NSStringFromCGRect(startRect));
// compute the end frame
CGRect pickerRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height - pickerSize.height,
pickerSize.width,
pickerSize.height+10);
NSLog(@"pickerRect %@",NSStringFromCGRect(pickerRect));
// start the slide up animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
// we need to perform some post operations after the animation is complete
[UIView setAnimationDelegate:self];
self.pickerView.frame = pickerRect;
[UIView commitAnimations];
}
}
I have tried increasing the frame size by 10 points (see CGSize pickerSize above) and thought I could use something like the following, but the button refuses to display, plus I'm not sure how to place the button inside the pickerSize frame itself. (BTW) The DatePicker and Button are overlaying a scrollView if that matters.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Done" forState:UIControlStateNormal];
button.center = CGPointMake(160,240);
[button addTarget:self action:@selector(done) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
Any thoughts are appreciated.
I customize Micah's class to support multiple orientation and ARC
DateTimePicker.h
DateTimePicker.m
Using custom view in your view controller:
Example code using UIToolbar as accessoryview:
Define instance variables keyboardToolbar and datePicker.
I've done basically the exact same thing. I subclassed UIView. It animates up and everything. Here's some code for you:
How about making a new UIView and put the datepicker and done button in it?
Based on above code, I made a repository on Git Hub:https://github.com/lenhhoxung86/CustomDatePicker