如何在iPhone应用程序中创建popover?

时间:2012-07-18 18:28:01

标签: ios iphone popover

我正在寻找iPhone中的popover,我想让它像iOS 5 Reader功能:

enter image description here

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

2 个答案:

答案 0 :(得分:26)

您可以使用某些自定义图片制作UIView并在视图顶部以动画形式显示它作为“popover”,其中包含一些按钮,如下所示:

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) {}];

如果您想使用#import <QuartzCore/QuartzCore.h>

,请不要忘记customView.layer

答案 1 :(得分:12)

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

你有它,漂亮,整齐模糊的popover。