How to create popover in iPhone app?

前端 未结 2 1286
时光说笑
时光说笑 2020-12-13 01:12

I\'m looking for popover in iPhone and i want to make it like iOS 5 Reader feature:

\"enter

2条回答
  •  轮回少年
    2020-12-13 01:50

    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.

提交回复
热议问题