3

我有一个 UIButton,单击它时会显示一个 UIPopover,其中包含一个 UIDatePicker。

它应该显示在出生日期的按钮下方,但它却覆盖了它。

这是我的代码:

- (IBAction)dateOfBirthButtonPressed:(id)sender{
    UIViewController* popoverContent = [[UIViewController alloc] init];
    UIView *popoverView = [[UIView alloc] init];
    popoverView.backgroundColor = [UIColor blackColor];

    UIDatePicker *datePickerTemp = [[UIDatePicker alloc]init];
    datePickerTemp.frame=CGRectMake(0,44,320, 216);
    datePickerTemp.datePickerMode = UIDatePickerModeDate;
    datePickerTemp.maximumDate = [NSDate date];
    self.datePicker = datePickerTemp;
    [popoverView addSubview:self.datePicker];

    UIToolbar *toolbar = [[UIToolbar alloc] init];
    toolbar.frame=CGRectMake(0,0 ,320, 40);
    toolbar.barStyle = UIBarStyleBlackOpaque;
    NSMutableArray *toolbarItems = [NSMutableArray array];
    UIBarButtonItem *cancelButton1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(datePickerCancelButtonClicked)];
    UIBarButtonItem *doneButton1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(datePickerSaveButtonClicked)];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [toolbarItems addObject:cancelButton1];
    [toolbarItems addObject:space];
    [toolbarItems addObject:doneButton1];
    toolbar.items = toolbarItems;
    [popoverView addSubview:toolbar];
    [cancelButton1 release];
    [space release];
    [toolbar release];

    popoverContent.view = popoverView;
    UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
    popoverController.delegate=self;
    self.datePopoverController = popoverController;
    [popoverContent release];
    [popoverView release];

    [self.datePopoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO];
    [self.datePopoverController presentPopoverFromRect:self.dateOfBirthButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

帧的 NSLog:

DOB button: {{160, 129}, {329, 37}}
Last name label: {{160, 77}, {329, 31}}

在此处输入图像描述

4

2 回答 2

1

您的按钮是视图的子视图,还是隐藏在另一个子视图中?如果它不是您视图的直接子视图,则您的坐标空间已关闭。尝试将您的代码更改为:

CGRect buttonRect = [self.dateOfBirthButton convertRect:self.dateOfBirthButton.frame toView:self.view];
[self.datePopoverController presentPopoverFromRect:buttonRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
于 2011-12-18T02:24:39.350 回答
0

您可以尝试其中任何一种来解决问题。第二个解决了我的问题,与您的问题相似。

[self.datePopoverController presentPopoverFromRect:self.dateOfBirthButton.frame inView:self.dateOfBirthButton allowedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

或者

[self.datePopoverController presentPopoverFromRect:self.dateOfBirthButton.frame inView:self.dateOfBirthButton.superview allowedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

于 2011-12-18T01:59:37.733 回答