creating button for popover view anchor at run time

后端 未结 4 2018
误落风尘
误落风尘 2021-02-18 20:38

This may not be possible, but I\'m hoping someone will have an idea how to do it.

I have an app I\'m porting from iPhone only to Universal. On the iPhone, I\'m using a

相关标签:
4条回答
  • 2021-02-18 21:09

    I was curious about this too so I made a quick test project. You're right, there doesn't seem to be a way to configure the popover segue at runtime or add an anchor point to a button that's not in the view hierarchy using Interface Builder.

    My solution was to set everything up in IB with the UIBarButtonItem visible and connected to an IBOutlet property, then remove it from the navigation bar in -viewDidLoad:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.navigationItem.rightBarButtonItem = nil;
    }
    

    Then I simply add it back or remove it by tapping another button:

    - (IBAction)toggleBarButtonItem:(id)sender
    {
        UIBarButtonItem *item = (self.navigationItem.rightBarButtonItem == nil) ?  self.popoverBarButtonItem : nil;
        [self.navigationItem setRightBarButtonItem:item animated:YES];
    }
    

    You could conditionally keep or remove the button in -viewDidLoad the same way. The segue remains anchored to the UIBarButtonItem.

    0 讨论(0)
  • 2021-02-18 21:15

    I'm gonna try making a dummy view that's the size and shape of the views I want to present the popover from, wire that to the segue popover target, and then move the view to the right position in prepareForSegue:sender:

    0 讨论(0)
  • 2021-02-18 21:29

    I had the same problem and solved it by creating a UIBarButtonItem in the Storyboard for the view controller but not part of the view hierarchy.

    In IB, Drag a bar button item to the dark bar below the view controller view, drop it next to the "First Responder" and "View Controller" icons. Create a (strong) IBOutlet for it. Then create a popover segue from it to the destination view controller by dragging from the bar button item to the destination. It seems like this is the only way to set it as the anchor. Choosing it as the anchor for an existing segue does not work (looks like an IB bug).

    enter image description here

    In viewDidLoad you can assign this bar button item to the navigationItem (or where ever you like) and the segue works as expected.

    0 讨论(0)
  • 2021-02-18 21:31

    I'm not sure this is exactly what you want, but this is what I would do. Create a button and set it up with some target/action. Call that target/action method

    presentPopover:(UIButton *)sender;
    

    Then in the presentPopover method, say

    UIViewController *customAdditionalViewController = [[MySpecialVC alloc] init];
    //Configure the customAdditionalViewController
    //Present the customAdditionalViewController in a Popover
    UIPopoverController *popover = [[UIPopoverController alloc] initWithViewController:customAdditionalViewController];
    //Set popover configurations
    [popover presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:/*whatever you want*/ animated:YES];
    

    That is how I would handle your use case.

    0 讨论(0)
提交回复
热议问题