我尝试将 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];
最佳答案
为什么没有为您的概念选择 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];
我得到了这样的输出
关于ios - 如何将 UIDatePicker 添加到 UIAlertController iOS9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39158822/