适用于 iPhone 和 iPad 的通用弹出视图,您可以在项目中使用。无需自定义绘制,无需自定义元素 - 一切都是纯粹的原生实现。
iPhone | iPad |
---|---|
ARSPopover 的示例使用可能如下所示
- (IBAction)showPopoverWithWebView:(id)sender {
ARSPopover *popoverController = [ARSPopover new];
popoverController.sourceView = self.buttonWithWebView;
popoverController.sourceRect = CGRectMake(CGRectGetMidX(self.buttonWithWebView.bounds), CGRectGetMaxY(self.buttonWithWebView.bounds), 0, 0);
popoverController.contentSize = CGSizeMake(400, 600);
popoverController.arrowDirection = UIPopoverArrowDirectionUp;
[self presentViewController:popoverController animated:YES completion:^{
[popoverController insertContentIntoPopover:^(ARSPopover *popover, CGSize popoverPresentedSize, CGFloat popoverArrowHeight) {
CGFloat originX = 0;
CGFloat originY = 0;
CGFloat width = popoverPresentedSize.width;
CGFloat height = popoverPresentedSize.height - popoverArrowHeight;
CGRect frame = CGRectMake(originX, originY, width, height);
UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
webView.scalesPageToFit = YES;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://google.com"]]];
[popover.view addSubview:webView];
}];
}];
}
为了获得一个有效的弹出视图,您需要指定以下属性
popoverController.sourceView
- 包含弹出视图锚定矩形的视图。popoverController.sourceView = self.buttonWithWebView;
popoverController.sourceRect
- 在指定视图中锚定弹出视图的矩形。popoverController.sourceRect = CGRectMake(CGRectGetMidX(self.buttonWithWebView.bounds), CGRectGetMaxY(self.buttonWithWebView.bounds), 0, 0);
popoverController.contentSize
- 弹出视图的偏好大小。popoverController.contentSize = CGSizeMake(400, 600);
insertContentIntoPopover
方法并传递一个代码块,该代码块应向您希望看到的弹出视图视图中添加子视图。请确保在弹出显示之后调用此方法。否则,您可能会在 popoverPresentedSize 中得到错误的大小。
[popoverController insertContentIntoPopover:^(ARSPopover *popover, CGSize popoverPresentedSize, CGFloat popoverArrowHeight) {
CGFloat originX = 0;
CGFloat originY = 0;
CGFloat width = popoverPresentedSize.width;
CGFloat height = popoverPresentedSize.height - popoverArrowHeight;
CGRect frame = CGRectMake(originX, originY, width, height);
UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
webView.scalesPageToFit = YES;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://google.com"]]];
[popover.view addSubview:webView];
}];
您可以通过实现以下代理方法,以使其符合 ARSPopoverDelegate
协议来接收从 UIPopoverPresentationController
的代理调用
- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing *)view;
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController;
- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController;
ARSPopover 根据 MIT 许可证 发布。有关详细信息,请参阅 LICENSE。