ios 如何在 iPhone 应用程序中创建弹出框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11547969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:18:20  来源:igfitidea点击:

How to create popover in iPhone app?

iosiphonepopover

提问by Xtrician

I'm looking for popover in iPhone and i want to make it like iOS 5 Reader feature:

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

enter image description here

在此处输入图片说明

After little research i found WEPopover and FPPopover but i'm looking if there anything like this API built-in iphone SDK.

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

回答by Aleksander Azizi

You could make a UIViewwith some custom artwork and display it with an animation on top of your view as a "popover" with some buttons like so:

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

Don't forget to #import <QuartzCore/QuartzCore.h>, if you want to use the customView.layer.

不要忘记#import <QuartzCore/QuartzCore.h>,如果您想使用customView.layer.

回答by Soberman

Since iOS8 we are now able to create popovers, that will be the same on iPhone, as on iPad, which would be especially awesome for those who make universal apps, thus no need to make separate views or code.

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

You can get the class as well as demo project here: https://github.com/soberman/ARSPopover

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

All you need to do is subclass UIViewController, conform to the UIPopoverPresentationControllerDelegateprotocol and set desired modalPresentationStylealong with the delegatevalue:

您需要做的就是 subclass UIViewController,符合UIPopoverPresentationControllerDelegate协议并设置所需modalPresentationStyledelegate值:

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

Afterwards, instantiate your newly created subclass in the method from which you want to show it and assign two more values to sourceViewand sourceRect. It looks like this:

随后,实例化的方法,从要显示它并指定两个值,新创建的子类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];

And there you have it, nice, neat blurred popover.

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