ios - 通用 UIPopoverPresentationController 代码

标签 ios objective-c uipopovercontroller

我创建了一个通用的 UIPopoverPresentationController。我将它放入具有 UIPopoverPresentationControllerDelegate 协议(protocol)的主视图 Controller 类中。这是一个如何从主视图 Controller 调用它的示例:

   [self presentPopoverOnSide:UIPopoverArrowDirectionDown target:viewArrowShouldPointAt storyboardID:@"Popover" popoverSize:CGSizeMake(200, 200)];

这是代码。这一个例程拥有弹出弹出窗口所需的一切。
- (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));
}

最佳答案

Swift 版本更具体:

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)

}

关于ios - 通用 UIPopoverPresentationController 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37512957/

相关文章:

objective-c - UIPopoverPresentationController 调暗背景更暗

ios - 如何在 SWIFT 的 sqlite 表中添加列

iphone - 使用不同的 XIB 启动应用程序

android - iOS 和 Android Max BLE 通告和扫描字节

ios - 异步服务调用的 TableView 单元测试

ios - 在点击 map 注释图钉时执行 segue

iphone - 适用于 iPhone 的 UIPopOverController(目前仅适用于 iPad)

ios - AFNetworking 返回 NSCFData; registerHTTPOperationClass 问题

ios - 可以左对齐按钮文本但右对齐背景或按钮图像吗?

objective-c - iOS:UIPopoverController 错误