UIPopoverController for iphone not working?

后端 未结 8 1759
日久生厌
日久生厌 2020-11-27 13:40

I need to use a UIPopOverController for my iPhone app ,i searched stackoverflow someone said UIPopoverController does not run on iphone iphone device WHY?.when i run on ipho

8条回答
  •  误落风尘
    2020-11-27 14:01

    Since iOS8 we are now able to create popovers, that will be the same on iPhone, as on iPad, which would be especially awesome for those who make universal apps, thus no need to make separate views or code.

    You can get the class as well as demo project here: https://github.com/soberman/ARSPopover

    All you need to do is subclass UIViewController, conform to the UIPopoverPresentationControllerDelegate protocol and set desired modalPresentationStyle along with the delegate value:

    // This is your CustomPopoverController.m
    
    @interface CustomPopoverController () 
    
    @end
    
    @implementation CustomPopoverController.m
    
    - (instancetype)init {
        if (self = [super init]) {
            self.modalPresentationStyle = UIModalPresentationPopover;
            self.popoverPresentationController.delegate = self;
        }
        return self;
    }
    
    - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
        return UIModalPresentationNone; //You have to specify this particular value in order to make it work on iPhone.
    }
    

    Afterwards, instantiate your newly created subclass in the method from which you want to show it and assign two more values to sourceView and sourceRect. It looks like this:

    CustomPopoverController *popoverController = [[CustomPopoverController alloc] init];
    popoverController.popoverPresentationController.sourceView = sourceView; //The view containing the anchor rectangle for the popover.
    popoverController.popoverPresentationController.sourceRect = CGRectMake(384, 40, 0, 0); //The rectangle in the specified view in which to anchor the popover.
    [self presentViewController:popoverController animated:YES completion:nil];
    

    And there you have it, nice, neat blurred popover.

提交回复
热议问题