3

I have a UIButton that when clicked, displays a UIPopover with a UIDatePicker in it.

It is supposed to display under button for date of birth, but it is covering it instead.

This is the code I have:

- (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

JOM
  • 8,139
  • 6
  • 78
  • 111
Jon
  • 4,732
  • 6
  • 44
  • 67
  • Your code looks correct. Could you perhaps `NSLog` the frame locations/sizes of the DOB button and the last name fields before calling `presentPopoverFromRect`? – Sergey Kalinichenko Dec 17 '11 at 22:01
  • Ok, thanks. I added the `NSLog` output in the question. – Jon Dec 17 '11 at 22:16
  • The logged numbers look suspicious: they say that the DOB button and the last name label are vertically aligned at 160, but the button (covered by the popover in the picture) is well to the right of the last name label. – Sergey Kalinichenko Dec 17 '11 at 22:27
  • Sorry the pic I used was a little old, let me upload the correct picture that goes with the code I posted. – Jon Dec 17 '11 at 22:29
  • Ok, I added the new picture, sorry about that. – Jon Dec 17 '11 at 22:31
  • Everything in your code looks fine to me. I'd try filing a bug with Apple, and in the meantime play around with the arrow direction (see what happens if you allow only the "down" direction), and also experiment with hard-coding of a smaller rectangle (say, a 2 by 2 at 300,267) and see what happens. – Sergey Kalinichenko Dec 17 '11 at 22:45
  • I think I figured it out. I had a UIView inside of a UIView in my XIB which was messing things up. – Jon Dec 17 '11 at 23:03
  • Great find! Good luck with your app! – Sergey Kalinichenko Dec 17 '11 at 23:05
  • Thanks, I got it working now, but when the screen is rotated while the popover is already displayed, it doesn't reposition it to under the DOB button, but it ends up a little above it. Do I have to add a method to reposition it after screen orientation/ – Jon Dec 17 '11 at 23:07
  • I would close the popover when the screen is about to rotate, and re-open at the new spot when the rotation has completed. – Sergey Kalinichenko Dec 17 '11 at 23:29

2 Answers2

1

Is your button a subview of your view, or buried within another subview? If it is not a direct subview of your view then your coordinate space is off. Try changing your code to:

CGRect buttonRect = [self.dateOfBirthButton convertRect:self.dateOfBirthButton.frame toView:self.view];
[self.datePopoverController presentPopoverFromRect:buttonRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
gschandler
  • 3,208
  • 16
  • 16
0

You can try either one of these to see that fix the problem. The second one fix my problem that is similar to yours.

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

or

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

Ken W
  • 999
  • 7
  • 10