How to add date picker to UIAlertController(UIAlertView) in Swift?

前端 未结 2 500
北荒
北荒 2021-01-15 05:07

I have an application in which I need to display a popup to pick a date and write that date back to the view controller.

I have searched online a lot but wasn\'t suc

相关标签:
2条回答
  • 2021-01-15 05:54

    Do not use any kind of UIAlertView or UIAlertController. Make your own view and pop it up (using, probably, a presented view controller). The view can be small, like an date picker, and you can put a shadow view behind it, like an alert does.

    0 讨论(0)
  • 2021-01-15 06:14

    This is my Code :

    - (id)initWithDatePicker:(NSString*)title parentView:(UIView*)parentView {
     self = [super init];
    
     if (self) {
         datePickerView = [[UIDatePicker alloc] init];
         datePickerView.datePickerMode = UIDatePickerModeDateAndTime;
         if (IS_IOS8_AND_UP) {
             alertViewController = [UIAlertController
                                    alertControllerWithTitle:EMPTY_STRING
                                    message:title
                                    preferredStyle:UIAlertControllerStyleActionSheet];
             UIView* aboveBlurLayer = alertViewController.view.subviews[0];
             [aboveBlurLayer addSubview:datePickerView];
             [aboveBlurLayer setWidth:SCREEN_WIDTH - 16];
             [datePickerView setWidth:SCREEN_WIDTH - 16];
             [alertViewController.view setWidth:SCREEN_WIDTH - 16];
             [alertViewController.view setBackgroundColor:[UIColor whiteColor]];
    
             UIAlertAction* alertAction =
             [UIAlertAction actionWithTitle:EMPTY_STRING
                                      style:UIAlertActionStyleDefault
                                    handler:nil];
             [alertViewController addAction:alertAction];
             [alertViewController addAction:alertAction];
             [alertViewController addAction:alertAction];
             [alertViewController addAction:alertAction];
    
             [datePickerView setBackgroundColor:[UIColor whiteColor]];
             [aboveBlurLayer addSubview:datePickerView];
         } else {
             actionSheet = [[UIActionSheet alloc] initWithTitle:title
                                                       delegate:self
                                              cancelButtonTitle:nil
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:nil];
    
             [actionSheet addSubview:datePickerView];
         }
    
         [self addToolBar];
         isDatePicker = YES;
         parent = parentView;
     }   
      return self; 
    }
    

    On Tool bar I have two buttons Done and Cancel On Done i send back a call via delegate with selected date and on cancel I dismiss. This code is for both iOS 7 and iOS 8

    0 讨论(0)
提交回复
热议问题