creating button for popover view anchor at run time

后端 未结 4 2003
误落风尘
误落风尘 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.

提交回复
热议问题