How to use Storyboard to make popover that can be used in code?

25,953

Solution 1

You can create segue that is not connected to any control but I don't think that there would be way to specify anchor point for popover from code. Another option is to create ViewController that is not connected with any segue. When editing storyboard, create ViewController which will be placed in popover, select it and navigate to Utilities pane->Attributes Inspector. Set Size to Freeform, Status Bar to None, specify unique Identifier that will be used to instantiate ViewController from code. Now you can change the size of ViewController by selecting its View and navigating to Utilities pane->Size Inspector.

After that you can create popover from code:

- (IBAction)buttonPressed:(id)sender {
    UIView *anchor = sender;
    UIViewController *viewControllerForPopover = 
        [self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"];

    popover = [[UIPopoverController alloc] 
               initWithContentViewController:viewControllerForPopover];
    [popover presentPopoverFromRect:anchor.frame 
                             inView:anchor.superview 
           permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

One caveat is that you need to hold reference to popover as ivar of your class, otherwise it'll crash because UIPopoverController would be released and deallocated after buttonPressed returns:

@interface MyViewController : UIViewController {
//  ...
    UIPopoverController *popover;
//  ...
}

Solution 2

So, I had a similar issue, and in case others might benefit, I figured I'd share it, since I benefit so much from stackoverflow.

This solution allows you to set the anchor of a customizable popover segue. It also allows you to configure the segue to be modal or not (I could not find a way to prevent the segue by dimming the exterior context, so if someone knows how to do that, I would be interested in hearing it); this is accomplished by setting the passthrough views for the popover controller. I also added the capacity to specify a custom view, rather than the view of the source viewcontroller (since I needed this capacity); this portion is not critical to the solution.

DynamicPopoverSegue.h

@interface DynamicPopoverSegue : UIStoryboardPopoverSegue

@property BOOL isModal;
@property UIView* sourceView;
@property CGRect anchor;

@end

DynamicPopoverSegue.m

@implementation DynamicPopoverSegue

- (void)perform
{
   if (!self.popoverController.popoverVisible)
   {
      UIViewController* dst = (UIViewController*)self.destinationViewController;
      UIViewController* src = (UIViewController*)self.sourceViewController;

      UIView* inView =  _sourceView ? _sourceView : src.view;

      self.popoverController.contentViewController = dst;
      if (!_isModal)
      {
         [self.popoverController setPassthroughViews:[[NSArray alloc] initWithObjects:inView, nil]];
      }
      [self.popoverController presentPopoverFromRect:_anchor
                                              inView:inView
                            permittedArrowDirections:UIPopoverArrowDirectionAny
                                            animated:YES];
   }
}

@end

Then you just set your segue to "Custom" in the storyboard, and set the segue class to "DynamicPopoverSegue". In my case, since I wanted to associate it with dynamic layers in a view, I could not set the anchor, so I created the segue by control clicking from the view controller icon in the bar beneath my view controller to the view controller I was using to present the popupover.

To call the popover segue:

[self performSegueWithIdentifier:@"MyPopoverSegue" sender:self];

And to configure the popover segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   if ([[segue identifier] isEqualToString:@"MyPopoverSegue"])
   {
      DynamicPopoverSegue* popoverSegue = (DynamicPopoverSegue*)segue;
      // set the anchor to wherever you want it to be
      popoverSegue.anchor = _destinationFrame;
   }
}
Share:
25,953
David U
Author by

David U

Updated on September 06, 2020

Comments

  • David U
    David U almost 4 years

    I'm building a collection of forms each of which contains several fields. Some of the fields are UITextFields that will display a date. I've created a new class called DatePickerTextField, a descendant of UITextField. When a DatePickerTextField is tapped I'd like for a UIDatePicker control to appear in a popover.

    My question is how do I use the storyboard to implement the popover? I can do a segue when there is a specific, visible control in the scene. But how do I represent a generic popover in the scene that I can attach to any instantiated DatePickerTextField that becomes active?

  • David U
    David U about 12 years
    Thanks. Exactly what I need. I'm going to try your first idea to see if the sender of the performSegueWithIdentifier call becomes the anchor. If that doesn't work I'll go with the second idea.
  • xaphod
    xaphod over 9 years
    This didn't work for me: once the popover is shown, touch outside of it does not close it like it should