【问题标题】:How to create popover in iPhone app?如何在 iPhone 应用程序中创建弹出框?
【发布时间】:2012-07-17 21:04:10
【问题描述】:

我正在寻找 iPhone 中的弹出框,我想让它像 iOS 5 阅读器功能:

经过一番研究,我发现了 WEPopover 和 FPPopover,但我正在寻找是否有类似这个 API 的内置 iphone SDK。

【问题讨论】:

标签: ios iphone popover


【解决方案1】:

您可以使用一些自定义艺术品制作UIView,并在您的视图顶部以动画形式将其显示为带有一些按钮的“弹出框”:

UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(25, 25, 100, 50)]; //<- change to where you want it to show.

//Set the customView properties
customView.alpha = 0.0;
customView.layer.cornerRadius = 5;
customView.layer.borderWidth = 1.5f;
customView.layer.masksToBounds = YES;

//Add the customView to the current view
[self.view addSubview:customView];

//Display the customView with animation
[UIView animateWithDuration:0.4 animations:^{
    [customView setAlpha:1.0];
} completion:^(BOOL finished) {}];

如果你想使用customView.layer,别忘了#import &lt;QuartzCore/QuartzCore.h&gt;

【讨论】:

  • 如果我想在其中添加按钮,问题是所有按钮都停留在一个地方,您知道如何将按钮并排放置吗?
  • [yourButton setFrame:CGRectMake(5 /*X*/, 2 /*Y*/, 10 /*Width*/, 20 /*Hight*/)]; // change the X(5) Y(2) for placement
  • @AleksanderAzizi:在您创建的“弹出框”视图左上角生成三角形的代码在哪里?
  • 我创建了一个带有按钮的自定义视图,当我单击该按钮时,应该关闭自定义视图并显示上一个视图控制器
【解决方案2】:

从 iOS8 开始,我们现在可以创建弹出框,这在 iPhone 上和在 iPad 上是一样的,这对于制作通用应用程序的人来说尤其棒,因此无需制作单独的视图或代码。

您可以在这里获取课程和演示项目:https://github.com/soberman/ARSPopover

您需要做的就是继承UIViewController,遵守UIPopoverPresentationControllerDelegate 协议并设置所需的modalPresentationStyle 以及delegate 值:

// This is your CustomPopoverController.m

@interface CustomPopoverController () <UIPopoverPresentationControllerDelegate>

@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.
}

然后,在您要显示它的方法中实例化您新创建的子类,并将另外两个值分配给sourceViewsourceRect。它看起来像这样:

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];

你有它,漂亮,整洁的模糊弹出框。

【讨论】:

  • 备注:self.modalPresentationStyle = UIModalPresentationPopover; 应该是第一个,因为在其他情况下popoverPresentationController 将为零
  • 太棒了。 API 自最初的问题以来已经发展,现在这应该是公认的答案。
猜你喜欢
  • 1970-01-01
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-20
  • 2015-10-18
相关资源
最近更新 更多