bri*_*ans 6 uidatepicker uialertview ios
我正在尝试将日期选择器添加到警报视图.我可以根据按钮点击将动画添加到屏幕上,我看到一个黑盒但没有日期选择器.
这是我到目前为止所拥有的......
- (IBAction)showDatePicker {
CGRect frame = CGRectMake(200, self.view.frame.size.height, self.view.frame.size.width, 0); //CGRectMake(225, 145, 260, 125);
UIPickerView *datePicker = [[UIPickerView alloc] initWithFrame:frame];
UIAlertView *showDateAlert = [[UIAlertView alloc]
initWithTitle:@"Enter the Code Date"
message:@"Sample message"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Update", nil];
[self.view addSubview:datePicker];
[UIView beginAnimations:@"slideIn" context:nil];
[datePicker setCenter:CGPointMake(datePicker.frame.origin.x, self.view.frame.size.height - datePicker.frame.size.height/2)];
[UIView commitAnimations];
}
Run Code Online (Sandbox Code Playgroud)
我发现了一个似乎正在工作的示例但我没有在警报框中看到任何对话框或按钮,只有日期选择器本身.我在这里错过了什么吗?
- (IBAction)showDatePicker {
CGRect frame = CGRectMake(225, self.view.frame.size.height, self.view.frame.size.width, 125);
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:frame];
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setDate:[NSDate date]];
UIAlertView *alert;
alert = [[UIAlertView alloc]
initWithTitle:@"Enter the Code Date"
message:@"Sample message"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Update", nil];
alert.delegate = self;
[alert addSubview:datePicker];
[alert show];
}
Run Code Online (Sandbox Code Playgroud)
小智 6
在斯威夫特:
let myDatePicker: UIDatePicker = UIDatePicker()
// setting properties of the datePicker
myDatePicker.timeZone = NSTimeZone.localTimeZone()
myDatePicker.frame = CGRectMake(0, 15, 270, 200)
let alertController = UIAlertController(title: "\n\n\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
alertController.view.addSubview(myDatePicker)
let somethingAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
alertController.addAction(somethingAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion:{})
Run Code Online (Sandbox Code Playgroud)
我同意评论者的观点,他们认为这不是实现这一目标的方法,原因有几个。首先,没有人喜欢不断弹出警报视图。其次,这不适用于您的情况,它最终可能会导致应用程序被拒绝。Apple 更改了类引用中的措辞,UIAlertView将其视图层次结构称为私有;我们都知道苹果对你乱搞他们认为私密的事情有何感想。
来自 UIAlertView 类参考:
UIAlertView 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。
但既然你说这是一个私人应用程序,那就这样吧。AUIAlertView只是一个UIView子类。因此,所有框架和边界规则仍然适用。以下是如何调整选择器框架和警报范围以挤入日期选择器的基本示例。
// Create alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
// Show alert (required for sizes to be available)
[alert show];
// Create date picker (could / should be an ivar)
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, alert.bounds.size.height, 320, 216)];
// Add picker to alert
[alert addSubview:picker];
// Adjust the alerts bounds
alert.bounds = CGRectMake(0, 0, 320 + 20, alert.bounds.size.height + 216 + 20);
Run Code Online (Sandbox Code Playgroud)
编辑
正如许多“如何向 a 中添加一些视图UIAlertView”问题的评论和新答案所指出的,此方法在 iOS7 及更高版本中不再有效。这确实应该被视为这从根本上来说是一个坏主意的证据。
另请注意在普遍流传的“解决方案”中使用 来setValue:forKey:解决无法修改苹果私有视图层次结构的“问题”。setValue:forKey:是引起私有 API 扫描器兴趣的方法之一。accessoryView去文档和标题中搜索UIAlertView。它不在文档中。标头中唯一相关的项目是名为 的 ivar _accessoryView,其标记为@private。
对于内部或私人应用程序,我认为还可以,只是还可以。
| 归档时间: |
|
| 查看次数: |
15460 次 |
| 最近记录: |