首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >泛型UIPopoverPresentationController代码

泛型UIPopoverPresentationController代码
EN

Stack Overflow用户
提问于 2016-05-30 01:40:38
回答 1查看 859关注 0票数 2

我已经创建了一个通用的UIPopoverPresentationController。我将其放入我的主视图控制器类中,该类具有UIPopoverPresentationControllerDelegate协议。下面是一个如何从主视图控制器调用它的示例:

代码语言:javascript
复制
   [self presentPopoverOnSide:UIPopoverArrowDirectionDown target:viewArrowShouldPointAt storyboardID:@"Popover" popoverSize:CGSizeMake(200, 200)];

下面是代码。这一个例程包含了弹出窗口所需的所有内容。

代码语言:javascript
复制
- (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));
}
EN

回答 1

Stack Overflow用户

发布于 2019-11-13 00:00:41

Swift版本更具体:

代码语言:javascript
复制
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)

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37512957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档