我创建了一个通用的UIPopoverPresentationController。我将它放入我的主视图控制器类,它具有UIPopoverPresentationControllerDelegate协议。以下是如何从主视图控制器调用它的示例:
[self presentPopoverOnSide:UIPopoverArrowDirectionDown target:viewArrowShouldPointAt storyboardID:@"Popover" popoverSize:CGSizeMake(200, 200)];
这是代码。这个例程有你需要提出一个popover。
- (void)presentPopoverOnSide:(UIPopoverArrowDirection)side target:(UIView *)target storyboardID:(NSString *)storyboardID popoverSize:(CGSize)popoverSize {
// get the popover view controller
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController * controller = [storyboard instantiateViewControllerWithIdentifier:storyboardID];
// set the popover view controller parameters
controller.modalPresentationStyle = UIModalPresentationPopover;
controller.preferredContentSize = popoverSize;
[self presentViewController:controller animated:YES completion:nil];
// set the parameters from the popover itself
UIPopoverPresentationController * popover = [controller popoverPresentationController];
popover.delegate = self; // my view controller is the <UIPopoverPresentationControllerDelegate>
// define where the popover arrow should point
popover.permittedArrowDirections = side;
popover.sourceView = target;
popover.sourceRect = CGRectMake(0, 0, target.frame.size.width, target.frame.size.height);
}
#pragma Mark ----------------- <UIPopoverPresentationControllerDelegate> methods -------------------
- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController {
NSLog(@"prepareForPopoverPresentation");
}
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
// only called when touchup outside the popover
NSLog(@"popoverPresentationControllerShouldDismissPopover");
return YES;
}
- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
// only called when touchup outside the popover
NSLog(@"popoverPresentationControllerDidDismissPopover");
}
- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing _Nonnull *)view {
NSLog(@"willRepositionPopoverToRect: %@", NSStringFromCGRect(*rect));
}
答案 0 :(得分:0)
更快速的版本更具体:
func showPathPopover(button: UIButton) {
let popController = self.storyboard?.instantiateViewController(withIdentifier: "PathPopover") as! PathPopover
popController.modalPresentationStyle = UIModalPresentationStyle.popover
popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.any
popController.popoverPresentationController?.delegate = self
popController.popoverPresentationController?.sourceView = button
popController.popoverPresentationController?.sourceRect = button.bounds
// present the popover
self.present(popController, animated: true, completion: nil)
}