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

后端 未结 5 2005
小鲜肉
小鲜肉 2020-12-24 03:56

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 cla

相关标签:
5条回答
  • 2020-12-24 04:24

    Add a UIView in the scene dock.

    You can add it as a subview to any existing view on the view controller.

    You can then toggle it's isHidden property as you require.

    You can add multiple such subviews and create many such popups.

    This technique will save you from setting up a new View Controller and using segues.

    0 讨论(0)
  • 2020-12-24 04:27

    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;
    //  ...
    }
    
    0 讨论(0)
  • 2020-12-24 04:30

    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;
       }
    }
    
    0 讨论(0)
  • 2020-12-24 04:34
    - (IBAction)pressItemChooseOprateRoom:(id)sender {
        if (isPad){
            // UIView *anchor = sender;
            UIViewController *viewControllerForPopover =
            [self.storyboard instantiateViewControllerWithIdentifier:@"OperateRoomList"];
    
            _myPopover = [[UIPopoverController alloc]
                          initWithContentViewController:viewControllerForPopover];
    
            CGRect rc=[self getBarItemRc:sender];
            [_myPopover presentPopoverFromRect:rc
                                        inView:self.view
                      permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    
            [MLControl shared].popover =self;
            // [self perfformSegueWithIdentifier:SEGUE_POP_OPERATEROOM sender:self];
        }else{
            [self iphoneOpenOperateRoomList];
            /* [self performSegueWithIdentifier:@"iPhonePushOperateRoom" sender:self];
             */
        }
    }
    
    -(void)iphoneOpenOperateRoomList{
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"OperateRoomList"];
        //        if (!index.showTabBar) {
        //            vc.hidesBottomBarWhenPushed = YES;
        //        }
    
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    0 讨论(0)
  • 2020-12-24 04:35

    Just used the answer from Jonnywho for my SWIFT project. In case you need it:

    Here's the SWIFT version:

        let anchor: UIView = sender
        var viewControllerForPopover = self.storyboard?.instantiateViewControllerWithIdentifier("GameAboutViewController") as! UIViewController?
    
    
        let popover = UIPopoverController(contentViewController: viewControllerForPopover!)
        popover.presentPopoverFromRect(anchor.frame, inView: anchor, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
    
    0 讨论(0)
提交回复
热议问题