如何将UIDatePicker添加到UIAlertController iOS9中

时间:2016-08-26 05:24:53

标签: ios objective-c uidatepicker uialertcontroller

我尝试将日期piker添加到alertcontroller但是在尺寸方面存在一些问题。我不知道如何使用日期piker的高度获得datepikerinit UIAlertController的高度。 所以我做了什么:

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"\n\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];

UIView *viewDatePicker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
[viewDatePicker setBackgroundColor:[UIColor clearColor]];

CGRect pickerFrame = CGRectMake(0, 0, self.view.frame.size.width, 200);
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[picker setDatePickerMode:UIDatePickerModeTime];

[viewDatePicker addSubview:picker];

[alertController.view addSubview:viewDatePicker];

NSDateFormatter *formate=[[NSDateFormatter alloc]init];
[formate setDateFormat:@"hh:mm a "];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * action) {

                                                          sinceDateString = [formate stringFromDate:picker.date];
                                                          [sinceDateButton setTitle:sinceDateString forState:UIControlStateNormal];

                                                      }];

[alertController addAction:defaultAction];
[self presentViewController:alertController animated:YES completion:nil];
  

我发现这个解决方案与UIAlertController相同   * alertController = [UIAlertController alertControllerWithTitle:nil message:@“\ n \ n \ n \ n \ n \ n \ n \ n \ n \ n”   preferredStyle:UIAlertControllerStyleActionSheet];

但我知道这是错误的解决方案,因为我看到灰线。 UIAlertController

2 个答案:

答案 0 :(得分:3)

为什么没有为您的概念选择UIDatePicker,请参阅此example

  

\ n是新行字符,它将位置移动到下一行的开头

并且你的第二个问题在中心显示灰线,这意味着你是日期选择区域的日期选择区域。

  

在您的代码中使用了 \ n \ n \ n \ n \ n \ n \ n \ n 10次,删除了两次   \ n \ n然后显示完美。

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];

UIView *viewDatePicker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,200)];
[viewDatePicker setBackgroundColor:[UIColor clearColor]];

CGRect pickerFrame = CGRectMake(0, 0, self.view.frame.size.width, 200);
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[picker setDatePickerMode:UIDatePickerModeTime];

[viewDatePicker addSubview:picker];

[alertController.view addSubview:viewDatePicker];

NSDateFormatter *formate=[[NSDateFormatter alloc]init];
[formate setDateFormat:@"hh:mm a"];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * action) {

                                                          //                                                              sinceDateString = [formate stringFromDate:picker.date];
                                                          //                                                              [sinceDateButton setTitle:sinceDateString forState:UIControlStateNormal];

                                                      }];

[alertController addAction:defaultAction];
[self presentViewController:alertController animated:YES completion:nil];

我的输出就像

enter image description here

答案 1 :(得分:1)

Apple始终不鼓励将UIDatePicker添加到操作表中。自iOS 7以来,Apple已经引入了内联日期选择器的使用(请参阅在日历应用程序中完成的操作)。

如果您设法使用UIAlertController破解解决方法,它可能会在未来的iOS版本中再次破解。