Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date Picker support for iOS 8 #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 49 additions & 29 deletions src/ios/DatePicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ @interface DatePicker ()

@property (nonatomic) BOOL isVisible;
@property (nonatomic) UIActionSheet* datePickerSheet;
@property (nonatomic) UIView* datePickerView;
@property (nonatomic) UIDatePicker* datePicker;
@property (nonatomic) UIPopoverController *datePickerPopover;

Expand All @@ -37,7 +38,8 @@ - (void)show:(CDVInvokedUrlCommand*)command {

- (BOOL)showForPhone:(NSMutableDictionary *)options {
if(!self.isVisible){
self.datePickerSheet = [self createActionSheet:options];

self.datePickerView = [self createDatePickerView:options];
self.isVisible = TRUE;
}
return true;
Expand All @@ -53,7 +55,13 @@ - (BOOL)showForPad:(NSMutableDictionary *)options {

- (void)hide {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self.datePickerSheet dismissWithClickedButtonIndex:0 animated:YES];
[UIView animateWithDuration:0.3 animations:^{
[self.datePickerView setFrame:CGRectOffset(self.datePickerView.frame, 0, 300)];
} completion:^(BOOL finished) {
[self.datePickerView removeFromSuperview];
self.isVisible = NO;
}];

} else {
[self.datePickerPopover dismissPopoverAnimated:YES];
}
Expand Down Expand Up @@ -102,36 +110,48 @@ - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverControl

#pragma mark - Factory methods

- (UIActionSheet *)createActionSheet:(NSMutableDictionary *)options {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
// date picker
CGRect frame = CGRectMake(0, 40, 0, 0);
if(!self.datePicker){
self.datePicker = [self createDatePicker: options frame:frame];
}
[self updateDatePicker:options];
[actionSheet addSubview: self.datePicker];
// cancel button
UISegmentedControl *cancelButton = [self createCancelButton:options];
[actionSheet addSubview:cancelButton];
// done button
UISegmentedControl *doneButton = [self createDoneButton:options];
[actionSheet addSubview:doneButton];
// show UIActionSheet
[actionSheet showInView:self.webView.superview];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

return actionSheet;
-(UIView *)createDatePickerView:(NSMutableDictionary *)options {

float viewHeight = 240;

// create view off screen
CGRect alertViewFrame = CGRectMake(0, self.webView.frame.size.height, self.webView.frame.size.width, viewHeight);
UIView *alertView = [[UIView alloc] initWithFrame:alertViewFrame];
[alertView setBackgroundColor:[UIColor whiteColor]];
[alertView setUserInteractionEnabled:YES];

CGRect frame = CGRectMake(0, 40, self.webView.frame.size.width, viewHeight);

if(!self.datePicker){
self.datePicker = [self createDatePicker: options frame:frame];
[self.datePicker setBackgroundColor:[UIColor whiteColor]];
[self.datePicker addTarget:self action:@selector(dateChangedAction:) forControlEvents:UIControlEventValueChanged];
}
[self updateDatePicker:options];
[alertView addSubview:self.datePicker];

// cancel button
UISegmentedControl *cancelButton = [self createCancelButton:options];
[alertView addSubview:cancelButton];
// done button
UISegmentedControl *doneButton = [self createDoneButton:options];
[alertView addSubview:doneButton];

[self.viewController.view insertSubview:alertView aboveSubview:self.webView];

// animate veiw into view
[UIView animateWithDuration:0.3 animations:^{
[alertView setFrame:CGRectMake(0, self.webView.frame.size.height - viewHeight, frame.size.width, frame.size.height)];
}];

return alertView;

}

- (UIPopoverController *)createPopover:(NSMutableDictionary *)options {

CGFloat pickerViewWidth = 320.0f;
CGFloat pickerViewWidth = self.webView.frame.size.width;
CGFloat pickerViewHeight = 216.0f;
UIView *datePickerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, pickerViewWidth, pickerViewHeight)];

Expand Down Expand Up @@ -249,7 +269,7 @@ - (UISegmentedControl *)createDoneButton:(NSMutableDictionary *)options {
CGSize size = button.bounds.size;
CGFloat width = size.width;
CGFloat height = size.height;
CGFloat xPos = 320 - width - 5; // 320 == width of DatePicker, 5 == offset to right side hand
CGFloat xPos = self.webView.frame.size.width - width - 5; // 320 == width of DatePicker, 5 == offset to right side hand
button.frame = CGRectMake(xPos, 7.0f, width, height);

[button addTarget:self action:@selector(doneAction:) forControlEvents:UIControlEventValueChanged];
Expand All @@ -267,4 +287,4 @@ - (UIColor *)colorFromHexString:(NSString *)hexString {
return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
}

@end
@end