Close button on adaptive popover

后端 未结 3 418
别跟我提以往
别跟我提以往 2021-02-05 11:42

In a storyboard I have a root view controller with a button which triggers a \'Present as Popover\' segue to a UINavigationController containing a UITableViewController. I want

3条回答
  •  温柔的废话
    2021-02-05 12:18

    I found that the accepted answer doesn't correctly display a "done" button when in Compact mode (e.g. iPhone) but remain as popover in Regular mode (e.g. iPad).

    The following code is the bare minimum to make this work – place these in the presenting view controller class.

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        UIViewController *dest = segue.destinationViewController;
        dest.popoverPresentationController.delegate = self;
    }
    
    - (UIViewController *)presentationController:(UIPresentationController *)controller viewControllerForAdaptivePresentationStyle:(UIModalPresentationStyle)style {
        UIViewController* presentedViewController = controller.presentedViewController;
        if ([controller isKindOfClass:[UIPopoverPresentationController class]] && style == UIModalPresentationFullScreen) {
            UINavigationController* navCtrl = [[UINavigationController alloc] initWithRootViewController:presentedViewController];
            UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismiss:)];
            presentedViewController.navigationItem.rightBarButtonItem = bbi;
            return navCtrl;
        }
        return presentedViewController;
    }
    
    -(IBAction)dismiss:(id)sender {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    

    Notice the three differences with the accepted answer:

    • We don't need to override adaptivePresentationStyleForPresentationController:
    • We check whether the presentation controller is the popover but requesting full-screen mode instead.
    • We return a new navigation controller instance instead of (incorrectly) casting the presented view controller as a navigation controller.

提交回复
热议问题