UIPopover无法从UIButton正确显示

时间:2011-12-17 21:54:42

标签: iphone objective-c uipopovercontroller uidatepicker fppopover

我有一个UIButton,当点击它时,会显示一个带有UIDatePicker的UIPopover。

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

这是我的代码:

- (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 for frame:

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

enter image description here

2 个答案:

答案 0 :(得分:1)

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

CGRect buttonRect = [self.dateOfBirthButton convertRect:self.dateOfBirthButton.frame toView:self.view];
[self.datePopoverController presentPopoverFromRect:buttonRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

答案 1 :(得分: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];