ios - 如何将 UIDatePicker 添加到 UIAlertController iOS9

标签 ios objective-c uidatepicker uialertcontroller

我尝试将 date piker 添加到 alertcontroller 中,但在大小方面存在一些问题。我不知道如何获取 datepiker 的高度或只是 init UIAlertController 与 date piker 的高度。 所以我做了什么:

    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];

I find this solution one of the same question UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"\n\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];

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

最佳答案

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

\n is the new line character ,it moves the position to starting of next line

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

in your code you used the \n\n\n\n\n\n\n\n in 10 times, remove two \n\n then shows perfect.

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

关于ios - 如何将 UIDatePicker 添加到 UIAlertController iOS9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39158822/

相关文章:

ios - 如何在用户在 Swift 中键入时调整 UITextField 的大小

ios - 从SQLite提取数据并且只想获取列ID的最后一个值

ios - UIPopOverController + UITableView - 选择单元格时隐藏弹出窗口

ios - 无需越狱访问应用程序文件夹

swift - 更改了 UIDatePick textColor 但 AM PM 颜色未更改

Jquery 日期选择器 z-index 问题

iOS 网络应用主体颜色

iphone - 声明两个属性并使用相同的键调用 objc_setAssociatedObject

ios - 静态菜单场景中的 SpriteKit 内存泄漏

ios - 从 UIDatePicker 中提取小时和分钟