UIPopoverPresentationController on iPhone doesn't produce popover

后端 未结 4 1526
孤独总比滥情好
孤独总比滥情好 2020-11-27 15:37

I\'m trying to implement the new UIPopoverPresentationController in my iPhone app (using Objective C). What I want is a simple popover with a tableview that ema

4条回答
  •  醉酒成梦
    2020-11-27 16:35

    Steps:

    A) Link your UIButton to the popover's view controller using the Present As Popover segue type. I actually had to create a new project to get this to appear but it's probably something to do with the base SDK.

    B) Make the View Controller containing the UIButton conform to the . E.g. In your MyViewController.m file add:

    @interface MyViewController () 
    

    C) Add the method below to the View Controller containing the UIButton:

    - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    
        return UIModalPresentationNone;
    }
    

    D) Add the following into your prepareForSegue:sender: replacing your segue.identifier check:

    if ([segue.identifier isEqualToString:@"CatSelectSegue"]) {
        UIViewController *dvc = segue.destinationViewController;
        UIPopoverPresentationController *controller = dvc.popoverPresentationController;
        if (controller) {
            controller.delegate = self;
        }
    }
    

    Code tested and proof it works:

    Popover on iPhone without 3rd Party Controls

    Edit: My test app TPOPViewController.m file where the magic happens:

    #import "TPOPViewController.h"
    
    @interface TPOPViewController () //, UIAdaptivePresentationControllerDelegate>
    
    @end
    
    @implementation TPOPViewController
    
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
        NSString *identifier = segue.identifier;
        if ([identifier isEqualToString:@"popover"]) {
            UIViewController *dvc = segue.destinationViewController;
            UIPopoverPresentationController *ppc = dvc.popoverPresentationController;
            if (ppc) {
                if ([sender isKindOfClass:[UIButton class]]) { // Assumes the popover is being triggered by a UIButton
                    ppc.sourceView = (UIButton *)sender;
                    ppc.sourceRect = [(UIButton *)sender bounds];
                }
                ppc.delegate = self;
            }
        }
    }
    
    - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    
        return UIModalPresentationNone;
    }
    
    @end
    

    My test storyboard as well:

    Popover on iPhone test storyboard

提交回复
热议问题