我正在努力使用UIDatePicker . 我想我可以将它添加到我当前的视图中,就像我普通的子视图一样,但它没有出现 . 经过一番搜索,我找到了将其放入popover的文档 . 我按照这些说明将其运行在iPad上 . 但是弹出窗口不适用于iPhone . 我需要做些什么才能让它在那里工作?为什么我不能像常规子视图一样简单地添加它?
这是我的iPad代码:
-(void)selectADate: (CGPoint)p
{
UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor blackColor];
UIDatePicker *datePicker=[[UIDatePicker alloc]init];//Date picker
datePicker.frame=CGRectMake(0,44,320, 216);
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setTag:10];
datePicker.date = self.displayedDate;
[datePicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;
UIPopoverController * popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
//popoverController.delegate=self;
self.datePopover = popoverController;
[popoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO];
CGRect popoverLocation = CGRectMake(p.x-160, p.y-200, 320, 216);
[popoverController presentPopoverFromRect:popoverLocation inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];//tempButton.frame where you need you can put that frame//
}
- (void)pickerChanged:(id)sender
{
self.displayedDate = [sender date];
[self showMonthContainingDate:[sender date]];
}
我的屏幕目前是一种显示一个月的日历页面 . 每天的销售是一个子视图 . 屏幕顶部是 Headers ,其中包含年,月等信息 . 当用户点击 Headers 时,会出现日期选择器,日历页面会显示与选择器中所选日期对应的所选月份突出显示的那天(当天的子视图的背景颜色不同) .
我不确定为什么我甚至需要一个弹出窗口,除非它只是提供一个空白视图,用于放置日历页面平铺视图前面的日期选择器 . 但是我不应该简单地将它添加为子视图然后调用bringSubviewToFront:?我尝试过,但它在iPad上无效,因此我使用了popover .
如果我在添加其他子视图时添加它,我可以显示它,但我希望在点击 Headers 时显示它 . 这是我的点击代码:
- (IBAction)tap:(UITapGestureRecognizer *)sender {
//tap gesture recognizer
//Assume the user tapped on a day cell.
for (UIView *dayCell in self.view.subviews) {
if (CGRectContainsPoint(dayCell.frame, [sender locationInView:self.view])) {
//found the cell that was touched.
//NSLog(@"Found a view containing the touch with coords. %@",NSStringFromCGRect(dayCell.frame));
for (UIView *insideView in dayCell.subviews) {
for (UILabel *dayElement in insideView.subviews) {
//found a subview of the day cell.
if ([dayElement isKindOfClass:[UILabel class]]) {
NSLog(@"Found a subview %@",dayElement.text);
}
}
}
}
if ([dayCell isKindOfClass:[UILabel class]]) {
// This is the heading label.
UILabel * headingLabel = (UILabel *) dayCell;
if (CGRectContainsPoint(headingLabel.frame, [sender locationInView:self.view])) {
NSLog(@"The heading was tapped");
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//iPhone code goes here
[self iphoneSelectADate:[sender locationInView:self.view]];
} else {
[self selectADate:[sender locationInView:self.view]];
}
}
}
}
}
这是目前非工作的iPhone代码:
-(void)iphoneSelectADate: (CGPoint)p
{
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 216, 320, 216)];
[datePicker setDate:[NSDate date]]; //This is the default
[datePicker setHidden:NO];
[self.view addSubview:datePicker];
}
如果我在调试器中单步执行,我会看到此代码已执行,但是日期选择器不会显示 .
3 回答
UIDatePicker
继承自UIView
,因此您可以像UILabel
一样添加它 . 确保你给它一个框架 .像这样的东西适用于iPhone屏幕的底部:
听起来你可能有一个子视图分层问题,但不是
UIDatePicker
问题 .如果这不起作用,请发布一些代码,我们将帮助您解决问题 .
不确定这是否有帮助,但是对于已经存在的视图的UI更改需要使用main_queue来调度,例如:
dispatch_async(dispatch_get_main_queue(), ^ { [self.view addSubview:yourSubview]; });
我想我可能已经找到了问题的关键 . 通过this document阅读各个地方放置方向相关的代码后,我决定
viewWillLayoutSubviews
. 我再次阅读了该文档并决定尝试将该代码放在viewWillAppear
中,这似乎解决了这个问题 .使用
viewWillAppear
的更改也需要willAnimateRotationToInterfaceOrientation:duration:
的实现看起来iPad需要
viewWIllLayoutSubviews
而不是iPhone,所以我也添加了这个:最后一点说明 . 有一种情况是设备的方向设置不正确 - 即在启动时iPhone上的风景 . 根据this answer to another question,iPhone总是以纵向发布,除非它是一个仅限风景的应用程序 .